Useful for providing context feedback etc.
VB Code:
'\\ API declarations Const LB_GETITEMHEIGHT = &H1A1 Const LB_GETTOPINDEX = &H18E Private Declare Function SendMessageByValLong Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Private Function ListIndexAtPosition(ByVal YPos As Single, lstFields As ListBox) As Long Dim lItemHeight As Long 'height of an entry In the listbox in pixels.. Dim lTopIndex As Long Dim lEntry As Long Dim nYpos As Integer On Error Resume Next '\\ 1 - Get the height of each item... lItemHeight = SendMessageByValLong(lstFields.hWnd, LB_GETITEMHEIGHT, 0, 0) lItemHeight = lItemHeight * Screen.TwipsPerPixelY '\\ 2 - Get index of the topmost item... lTopIndex = SendMessageByValLong(lstFields.hWnd, LB_GETTOPINDEX, 0, 0) '\\ Find the Y position in pixels lEntry = lTopIndex + (YPos \ lItemHeight) ListIndexAtPosition = lEntry End Function
Example of use...
VB Code:
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.Caption = "Over index " & ListIndexAtPosition(Y, List1) End Sub


Reply With Quote