Snipplet - Get the item of a ListBox the mouse is over
Here's a short code snipplet you can use to find the item in a ListBox the mouse is over. This example will select the item if you right click on it, but you could just as well use the code in the MouseMove event instead to find the item the mouse is over, if you for example wanted to set the ToolTipText to the item text (could be good if the text is long and doesn't fit inside the listbox).
VB Code:
Private Declare Function SendMessageNull _
Lib "user32.dll" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Const LB_GETITEMHEIGHT As Long = &H1A1
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim nHeight As Long
Dim nItem As Long
If Button = vbRightButton Then
'calculate which item we have clicked on
nHeight = SendMessageNull(List1.hwnd, LB_GETITEMHEIGHT, 0, 0) _
* Screen.TwipsPerPixelY
nItem = List1.TopIndex + (Y \ nHeight)
'check if we actually clicked on an item
If nItem < List1.ListCount Then
List1.ListIndex = nItem
'You could for example pop-up a menu here
End If
End If
End Sub