Results 1 to 3 of 3

Thread: Item from Point on listbox [Resolved]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Resolved 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

  2. #2
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    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:
    1. Dim index As Integer = Math.Floor(e.Y / ListBox1.ItemHeight)
    2.         index += ListBox1.TopIndex
    3.         If index >= ListBox1.Items.Count Then
    4.             ToolTip1.SetToolTip(ListBox1, "")
    5.         Else
    6.             ToolTip1.SetToolTip(ListBox1, ListBox1.Items(index))
    7.         End If

    EDIT NOTE: This code is for the MouseMove event on ListBox1
    Last edited by Phill64; Oct 10th, 2005 at 03:49 AM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    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:
    1. Dim index As Integer = Math.Floor(e.Y / ListBox1.ItemHeight)
    2.         index += ListBox1.TopIndex
    3.         If index >= ListBox1.Items.Count Then
    4.             ToolTip1.SetToolTip(ListBox1, "")
    5.         Else
    6.             ToolTip1.SetToolTip(ListBox1, ListBox1.Items(index))
    7.         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:
    1. Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
    2.         Dim index As Integer = Math.Floor(e.Y / MyBase.ItemHeight)
    3.         index += MyBase.TopIndex
    4.         If index >= MyBase.Items.Count Then
    5.             '-No items under mouse-
    6.             Debug.WriteLine("White Space")
    7.         Else
    8.             '-Item under mouse-
    9.             'index = index of item under mouse
    10.  
    11.             'Since this list contains objects, not strings,
    12.             'we have to use ToString
    13.             Debug.WriteLine("item: " & MyBase.Items(index).ToString)
    14.         End If
    15.     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
  •  



Click Here to Expand Forum to Full Width