|
-
Oct 10th, 2005, 02:59 AM
#1
Thread Starter
Fanatic Member
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)?
Last edited by agent; Oct 10th, 2005 at 03:59 AM.
Reason: Resolved
-
Oct 10th, 2005, 03:41 AM
#2
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
Last edited by Phill64; Oct 10th, 2005 at 03:49 AM.
-
Oct 10th, 2005, 03:58 AM
#3
Thread Starter
Fanatic Member
Re: Item from Point on listbox
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|