Detect On Which Item The Mouse Is Hover On ListBox

'Add a module to your project (In the menu choose Project -> Add Module, Then click Open)
'Add ListBox to your form (named List1), and add some items to the ListBox's list.
'When you will move your mouse over one of the listbox's items,
'A ToolTipText will pop-up with the name of the item.
'Insert the following code to your module:

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd _
As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const LB_ITEMFROMPOINT = &H1A9

'Insert the following code to your form:

Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lXPoint As Long
Dim lYPoint As Long
Dim lIndex As Long
If Button = 0 Then
lXPoint = CLng(X / Screen.TwipsPerPixelX)
lYPoint = CLng(Y / Screen.TwipsPerPixelY)
With List1
  lIndex = SendMessage(.hwnd, LB_ITEMFROMPOINT, 0, ByVal _
  ((lYPoint * 65536) + lXPoint))
  If (lIndex >= 0) And (lIndex <= .ListCount) Then
' .List(lIndex) contains the name of the item under the mouse cursor.
' if you want to display the item in textbox, simply write:
' Text1.Text =.List(lIndex)  

   .ToolTipText = .List(lIndex)
  Else
   .ToolTipText = ""
  End If
End With
End If
End Sub

Go Back