ListBox: How to only fire SelectedIndexChanged if the an item is directly selected.

  • Thread starter Thread starter felix
  • Start date Start date
F

felix

I would like to only fire SelectedIndexChanged if the an item is
directly selected.

example:
I have a listbox with a few items in it. These items only half fill the
listbox. If I click in the listbox, but not directly on an item (that
is in the bottom half of the listbox), the bottom item is selected.
Any ideas?

Thanks!

Felix
 
You can't overload the event, but you can write some code in the event to
manage this. If you use the ListBox.IndexFromPoint() method, you can
determine if there is an actual ListBox item under the cursor when it's
selected. The method returns "ListBox.NoMatches" if the mouse isn't over an
item in your list, so you can just put some conditional logic in your
SelectedIndexChanged event (looks something like this):

Dim pt as Point = <form>.PointToClient(Cursor.Position)
If <listbox>.IndexFromPoint(pt) <> ListBox.NoMatches Then
'do your processing
End If

Where you would replace <form> with the name of the containing form, and
<listbox> with the name of your ListBox.

HTH

- Scott
 
Thanks, but it did not help.

What I have noticed though, is I only see my problem/bug when I have
DrawMode set to OwnerDrawVariable. If the DrawMode is Fixed or
OwnerDrawFixed than the SelectedIndexChanged event is not fired
inappropriately.

Anyone have any insight, or a workaround?

Thanks!
 
Back
Top