VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



How to get the item in a listbox that the mouse is currently over top of.

by Jon Mooty AKA YoungBuck (5 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Sun 9th September 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

How to get the item in a listbox that the mouse is currently over top of.

API Declarations


Private Const LB_GETITEMRECT = &H198
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

' You will also need a listbox names List1

Rate How to get the item in a listbox that the mouse is currently over top of.



 Dim i As Integer

 ' populate the listbox with the indexes for each entry
 For i = 0 To 50
 
    List1.AddItem i, i

 Next i
 
End Sub

Private Sub List1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    
    ' select the item the mouse is currently over
    List1.ListIndex = GetListItemFromPt(List1, x, y)
    
End Sub

Public Function GetListItemFromPt(plist As ListBox, x As Single, y As Single) As Integer
 Dim rCur As RECT
 Dim iIndex As Integer
 Dim iPixX As Integer, iPixY As Integer
 
 ' convert the coordinates to pixels (remove the next two lines if _
   you will be passing the coordinates in pixels)
 iPixX = Me.ScaleX(x, vbTwips, vbPixels)
 iPixY = Me.ScaleY(y, vbTwips, vbPixels)
 
 ' loop through the items of the listbox
 For iIndex = 0 To plist.ListCount - 1
    
    ' get the coordinates for each item in the listbox in its current _
      state, if Top is less than 0 or Bottom is greater than the height of _
      the listbox then the item is currently offscreen
    SendMessage plist.hwnd, LB_GETITEMRECT, iIndex, rCur
    
    ' if passed corrdinates are within the bounds of the current _
      item than exit the loop and return the index
    If iPixY >= rCur.Top And iPixY <= rCur.Bottom Then
        Exit For
    End If
        
    Next iIndex
    
    GetListItemFromPt = iIndex
        
End Function

Download this snippet    Add to My Saved Code

How to get the item in a listbox that the mouse is currently over top of. Comments

No comments have been posted about How to get the item in a listbox that the mouse is currently over top of.. Why not be the first to post a comment about How to get the item in a listbox that the mouse is currently over top of..

Post your comment

Subject:
Message:
0/1000 characters