Useful for providing context feedback etc.

VB Code:
  1. '\\ API declarations
  2. Const LB_GETITEMHEIGHT = &H1A1
  3. Const LB_GETTOPINDEX = &H18E
  4.  
  5. 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
  6.  
  7. Private Function ListIndexAtPosition(ByVal YPos As Single, lstFields As ListBox) As Long
  8.     Dim lItemHeight As Long 'height of an entry In the listbox in pixels..
  9.     Dim lTopIndex As Long
  10.     Dim lEntry As Long
  11.     Dim nYpos As Integer
  12.    
  13.     On Error Resume Next
  14.     '\\ 1 - Get the height of each item...
  15.     lItemHeight = SendMessageByValLong(lstFields.hWnd, LB_GETITEMHEIGHT, 0, 0)
  16.     lItemHeight = lItemHeight * Screen.TwipsPerPixelY
  17.     '\\ 2 - Get index of the topmost item...
  18.     lTopIndex = SendMessageByValLong(lstFields.hWnd, LB_GETTOPINDEX, 0, 0)
  19.  
  20.     '\\ Find the Y position in pixels
  21.     lEntry = lTopIndex + (YPos \ lItemHeight)
  22.     ListIndexAtPosition = lEntry
  23.    
  24. End Function

Example of use...
VB Code:
  1. Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2.  
  3. Label1.Caption = "Over index " & ListIndexAtPosition(Y, List1)
  4.  
  5. End Sub