Item from Point on listbox [Resolved]
As in understand it, the ListView control has a hittest method that would return one of the contained items give a point.
I have this really cool owner draw listbox that I've spent a while building and testing. After re-checking the spec, the app needs to popup a tool tip relevent to the item that the mouse is hovering over.
'No problem', I said, just before realizing the listbox has no hittest method.
Does anybody hear know of any way to find out which item (if any) the mouse cursor is over in a listbox (not a listview)?
Re: Item from Point on listbox
Here is something i threw together for you, i have never tried this before so thought id give it a shot, and it worked well.
this works using a calculation with ItemHeight and e.Y in the mousemove event, then adjusting with the TopIndex property.
VB Code:
Dim index As Integer = Math.Floor(e.Y / ListBox1.ItemHeight)
index += ListBox1.TopIndex
If index >= ListBox1.Items.Count Then
ToolTip1.SetToolTip(ListBox1, "")
Else
ToolTip1.SetToolTip(ListBox1, ListBox1.Items(index))
End If
EDIT NOTE: This code is for the MouseMove event on ListBox1
Re: Item from Point on listbox
Quote:
Originally Posted by Phill64
Here is something i threw together for you, i have never tried this before so thought id give it a shot, and it worked well.
this works using a calculation with ItemHeight and e.Y in the mousemove event, then adjusting with the TopIndex property.
VB Code:
Dim index As Integer = Math.Floor(e.Y / ListBox1.ItemHeight)
index += ListBox1.TopIndex
If index >= ListBox1.Items.Count Then
ToolTip1.SetToolTip(ListBox1, "")
Else
ToolTip1.SetToolTip(ListBox1, ListBox1.Items(index))
End If
EDIT NOTE: This code is for the MouseMove event on ListBox1
Awesome. Modified for testing as follows. I'll add some keywords for other people to search by at the bottom. This is excellent.
VB Code:
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim index As Integer = Math.Floor(e.Y / MyBase.ItemHeight)
index += MyBase.TopIndex
If index >= MyBase.Items.Count Then
'-No items under mouse-
Debug.WriteLine("White Space")
Else
'-Item under mouse-
'index = index of item under mouse
'Since this list contains objects, not strings,
'we have to use ToString
Debug.WriteLine("item: " & MyBase.Items(index).ToString)
End If
End Sub
Some search keywords: hittest item from mouse position item from point mouse cursor list box