Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a ListBox display a different tooltip for each item under the mouse in Visual Basic 6
DescriptionThis example shows how to make a ListBox display a different tooltip for each item under the mouse in Visual Basic 6.
KeywordsListBox, select, autoselect, ClientToScreen, LBItemFromPt
CategoriesControls, API
 
The ItemUnderMouse function returns the index of the item under the mouse. It uses the ClientToScreen API function to convert the (X, Y) coordinates of a point in the ListBox's coordinate system into screen coordinates. It then calls the LBItemFromPt API function to get the index of the item at that point.
 
' Return the index of the item under the mouse.
Public Function ItemUnderMouse(ByVal list_hWnd As Long, _
    ByVal X As Single, ByVal Y As Single)
Dim pt As POINTAPI

    pt.X = X \ Screen.TwipsPerPixelX
    pt.Y = Y \ Screen.TwipsPerPixelY
    ClientToScreen list_hWnd, pt
    ItemUnderMouse = LBItemFromPt(list_hWnd, pt.X, pt.Y, _
        False)
End Function
 
The form's Load event handler initializes the array m_TooltipText, which holds tooltips for the items. It also contains a tooltip for item index -1 to use when the mouse is not over any item. You might want this tooltip to be blank but in this example it gives a generic "Select an item" prompt.

The ListBox's MouseMove event handler calls ItemUnderMouse to see what item is under the mouse and then sets the control's tooltip to that item's m_TooltipText value.

 
' See which item is under the mouse and display its tooltip.
Private Sub List1_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    List1.ToolTipText = _
        m_TooltipText(ItemUnderMouse(List1.hwnd, X, Y))
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated