Knowing if the mouse is hovering over a particular item in listbox

Winston

Junior Contributor
Joined
Jan 25, 2003
Messages
266
Location
Sydney, Australia
I'm currently owner drawing a listbox, and extending it through the drawitem, and measure item methods, basically i've been trying many ways to detect if a particular list box item has i mouse over it, i've tried the e.state property using a bit-wise comparison through the enum, DrawItemState.HotLight. Doesn't work

Has anyone else got any better ideas?

Thanks.
 
This code demonstrates how you can determine what item the mouse is over, if any. Here, the ListBox is called LBox:
Visual Basic:
        Dim HoverItem As Object = Nothing
        Dim MP As Point = LBox.PointToClient(Control.MousePosition)
        Dim I As Integer
        For I = 0 To LBox.Items.Count - 1
            If LBox.GetItemRectangle(I).Contains(MP) Then
                'this item is the one being hovered
                HoverItem = LBox.Items(I)
                Exit For
            End If
        Next
        'At this point, HoverItem is Nothing if there is no item under the mouse,
        'or it'll be a reference to the item that is under the mouse
        'So you can insert whatever code you want here

If you were expecting C#, then I don't think it should be too difficult to see what the code above is trying to do...
 
Thanks this function does some what work...

ok here's what i got

Function mouseOnItem(ByVal index As Integer) As Boolean
Dim mousePos As Point = Me.ListBox1.PointToClient(Me.ListBox1.MousePosition)
If Me.ListBox1.GetItemRectangle(index).Contains(mousePos) Then
Return True
Else
Return False
End If
End Function

and i have a overrided the DrawItem event, and inside the draw function, i have some code that checks if it's over if it is draw a different colour rectangle etc...

however it seems to not work too, well, like only when i select an item is when it does in fact determine the mouse is over.

for example:

If me.mouseOnItem(e.index) then
'code
End If

that call only invokes 'code when a user selects a listbox item.

Any ideas?

Thanks
 
Your function will return True if the mouse is over the item. It looks to me like your code isn't even running until the item is clicked, which is no fault of the function. Where exactly is the code in question?
 
Well actualy i'm confused if it's the function or something else, ok, if you coudl

make a sample solution, chuck in a label and a listbox control

paste the function above in, in the listbox add a few items in the Items property, susbscribe to the DrawItem event for the listbox, and inside there, put in:

Me.Label.text = mouseOnItem(e.index)

now run the solution, and hover over it, youll notice the value doesnt change in the label at all even if u select the item, it's strange =\
 
Try using Mousemove or MouseUp/Down events

Tygurs works...

Visual Basic:
    Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove
        Dim HoverItem As Object = Nothing
        Dim MP As Point = ListBox1.PointToClient(Control.MousePosition)
        Dim ItemRep As Integer
        'for each item in the listbox
        For ItemRep = 0 To ListBox1.Items.Count - 1
            'if the mouse pointer is in the items draw rectange (it's being hovered)
            If ListBox1.GetItemRectangle(ItemRep).Contains(MP) Then
                'set the HoverItem object to the object being hovered
                HoverItem = ListBox1.Items(ItemRep)
                'boolean test here (if required)
                Me.Text = HoverItem.ToString & " index = " & ItemRep
                Exit For
            End If
        Next
    End Sub

For Listviews remember to use GetItemRect instead
 
Back
Top