CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Nov 2006
    Posts
    118

    Tooltips for each ListBox Item in visual basic 6

    Can anybody Suggest How to add ToolTip In the Combox while It is droped down. So that when we move mouse on the drop down box we can see the combo box s item in the tooltip

  2. #2
    Join Date
    Oct 2006
    Posts
    327

    Re: Tooltips for each ListBox Item in visual basic 6

    Your title says "listbox" while you question is concerning a "combobox" !!!
    Now : it is possible to do it with a listbox and it is not with a combobox (for the simple reason that a listbox responds to the mouse-move event while a combobox does not ...)
    So .... Is it a listbox or a combobox ?

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Tooltips for each ListBox Item in visual basic 6

    Yeah, there's a difference between ComboBox and ListBox

    This is how to do it with a ListBox :
    Code:
    Option Explicit
    
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
    
    Private Const LB_ITEMFROMPOINT = &H1A9
    
    Private Sub Form_Load()
        Dim iIndex As Integer
        
        For iIndex = 1 To 100
            List1.AddItem "List Item " & iIndex
        Next
    End Sub
    
    Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim tPOINT As POINTAPI
        Dim iIndex As Long
    
        'get the Mouse Cursor Position
        Call GetCursorPos(tPOINT)
        'Convert the Coords to be Relative to the Listbox
        Call ScreenToClient(List1.hWnd, tPOINT)
        'Find which Item the Mouse is Over
        iIndex = SendMessage(List1.hWnd, LB_ITEMFROMPOINT, 0&, ByVal ((tPOINT.X And &HFF) Or (&H10000 * (tPOINT.Y And &HFF))))
        If iIndex >= 0 Then
            'Extract the List Index
            iIndex = iIndex And &HFF
            'set the Lists ToolTipText
            List1.ToolTipText = List1.List(iIndex)
        End If
    End Sub
    Now, if I were to do the same (well almost the same ) with the combobox :
    Code:
    Option Explicit
    
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
    
    Private Const LB_ITEMFROMPOINT = &H1A9
    Const CB_SHOWDROPDOWN = &H14F
    
    'to open cb list automatically
    Sub ComboBoxOpenList(cbo As ComboBox, Optional showIt As Boolean = True)
        SendMessage cbo.hWnd, CB_SHOWDROPDOWN, showIt, ByVal 0&
    End Sub
    
    Private Sub Combo1_DropDown() 'drop down event
        Dim tPOINT As POINTAPI
        Dim iIndex As Long
    
        'get the Mouse Cursor Position
        Call GetCursorPos(tPOINT)
        'Convert the Coords to be Relative to the Listbox
        Call ScreenToClient(Combo1.hWnd, tPOINT)
        'Find which Item the Mouse is Over
        iIndex = SendMessage(Combo1.hWnd, LB_ITEMFROMPOINT, 0&, ByVal ((tPOINT.X And &HFF) Or (&H10000 * (tPOINT.Y And &HFF))))
        If iIndex >= 0 Then
            'Extract the List Index
            iIndex = iIndex And &HFF
            'set the Lists ToolTipText
            Combo1.ToolTipText = Combo1.List(iIndex)
        End If
    
    End Sub
    
    Private Sub Form_Load()
        Dim iIndex As Integer
        'Fill the List with Dummy Values
        For iIndex = 1 To 100
            Combo1.AddItem "Item " & iIndex
        Next
        ComboBoxOpenList Combo1, True 'automatically open dropdown list of combo
    End Sub
    The problem here is that it will ONLY show the tooltips when the list isn't dropped anymore.
    Last edited by HanneSThEGreaT; February 5th, 2007 at 02:33 PM. Reason: Typo

  4. #4
    Join Date
    Oct 2006
    Posts
    327

    Re: Tooltips for each ListBox Item in visual basic 6

    I have a different way to do it with a listbox (without Api)
    It would be possible to do it with a ComboBox and Apis, but at a very high cost in terms of code and execution time !

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Tooltips for each ListBox Item in visual basic 6

    Quote Originally Posted by moa
    I have a different way to do it with a listbox (without Api)
    Please do post

  6. #6
    Join Date
    Oct 2006
    Posts
    327

    Re: Tooltips for each ListBox Item in visual basic 6

    Quote Originally Posted by HanneSThEGreaT
    Please do post
    OK, I love the challenges
    Back in about minutes, then (the time I need to be sure I am not wrong)

  7. #7
    Join Date
    Oct 2006
    Posts
    327

    Re: Tooltips for each ListBox Item in visual basic 6

    Well...
    It finaly took less time that what I thought :

    Code:
    Private h As Single
    
    Private Sub Form_Activate()
      'All this in blue) is here only to make it easier for you to check
      'you can, for instance, change the fontsize
      List1.FontSize = 10
      For I = 0 To 40
        List1.AddItem I & "a"
      Next
      With Font
        Set Font = List1.Font
      End With
      h = TextHeight("aaa") * 1.02
    End Sub
    
    Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
         where_am_i = List1.TopIndex + Int(Y / h)
         List1.ToolTipText = List1.List(where_am_i)
    End Sub
    What about the idea ?
    Last edited by moa; February 5th, 2007 at 11:16 AM.

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Tooltips for each ListBox Item in visual basic 6

    Quote Originally Posted by moa
    What about the idea ?
    Nice approach!

  9. #9
    Join Date
    Aug 2006
    Posts
    145

    Re: Tooltips for each ListBox Item in visual basic 6

    if you're attempting to use the ToolTipText to show the full text of items wider than the dropdown, then I would recommend using CB_SETDROPPEDWIDTH to change the width of the dropdown instead:
    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
        ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Const CB_SETDROPPEDWIDTH As Long = &H160&
    
    Private Sub Combo1_DropDown()
        Dim N As Long, lLen As Long, lLongest As Long
        With Combo1
            Set Me.Font = .Font
            For N = 0 To .ListCount - 1
                lLen = Me.TextWidth(.List(N))
                If lLen > lLongest Then lLongest = lLen
            Next N
            SendMessage .hwnd, CB_SETDROPPEDWIDTH, Me.ScaleX(lLongest, Me.ScaleMode, vbPixels) + 30, 0&
        End With
    End Sub
    
    Private Sub Form_Load()
        Dim N As Long
        For N = 1 To 20
            Combo1.AddItem String$(40 * Rnd, "A")
        Next N
    End Sub
    obviously, depending on your circumstances, the code to determine the width should be just calculated once, rather than everytime it needs to drop down. The 30 in that Sub is also just a completely arbitrary number to allow for scrollbar / border (you could calculate it properly using GetSystemMetrics if you wished)

  10. #10
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Tooltips for each ListBox Item in visual basic 6

    Nicely done!

  11. #11
    Join Date
    Oct 2006
    Posts
    327

    Re: Tooltips for each ListBox Item in visual basic 6

    It is definitely very very nice !

    May I make a confession, now ?

    The old man I am (I am really old) was on the point to :

    - remind that a combobox is, finaly, nothing else but the combination of a textbox and a listbox
    - then propose to combinate a textbox and a listbox and treats the listbox

    I suppose one becomes lazzy when getting aged !....

  12. #12
    Join Date
    Jan 2017
    Posts
    10

    Re: Tooltips for each ListBox Item in visual basic 6

    Sorry to say but this method is BUGGY. It sometimes translates to the wrong list index. Try this one:

    Top of Module ...
    Code:
    Private Type POINTAPI
                        X As Long
                        Y As Long
    End Type
    
    Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    Private Declare Function LBItemFromPt Lib "COMCTL32.DLL" (ByVal hLB As Long, ByVal ptX As Long, ByVal ptY As Long, ByVal bAutoScroll As Long) As Long

    Code:
    Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    On Error Resume Next
    
    List1.ToolTipText = L.List(ItemUnderMouse(List1.hwnd, X, Y))
    End Sub
    Code:
    Private 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






    Quote Originally Posted by HanneSThEGreaT View Post
    Yeah, there's a difference between ComboBox and ListBox

    This is how to do it with a ListBox :
    Code:
    Option Explicit
    
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
    
    Private Const LB_ITEMFROMPOINT = &H1A9
    
    Private Sub Form_Load()
        Dim iIndex As Integer
        
        For iIndex = 1 To 100
            List1.AddItem "List Item " & iIndex
        Next
    End Sub
    
    Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim tPOINT As POINTAPI
        Dim iIndex As Long
    
        'get the Mouse Cursor Position
        Call GetCursorPos(tPOINT)
        'Convert the Coords to be Relative to the Listbox
        Call ScreenToClient(List1.hWnd, tPOINT)
        'Find which Item the Mouse is Over
        iIndex = SendMessage(List1.hWnd, LB_ITEMFROMPOINT, 0&, ByVal ((tPOINT.X And &HFF) Or (&H10000 * (tPOINT.Y And &HFF))))
        If iIndex >= 0 Then
            'Extract the List Index
            iIndex = iIndex And &HFF
            'set the Lists ToolTipText
            List1.ToolTipText = List1.List(iIndex)
        End If
    End Sub
    Now, if I were to do the same (well almost the same ) with the combobox :
    Code:
    Option Explicit
    
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
    
    Private Const LB_ITEMFROMPOINT = &H1A9
    Const CB_SHOWDROPDOWN = &H14F
    
    'to open cb list automatically
    Sub ComboBoxOpenList(cbo As ComboBox, Optional showIt As Boolean = True)
        SendMessage cbo.hWnd, CB_SHOWDROPDOWN, showIt, ByVal 0&
    End Sub
    
    Private Sub Combo1_DropDown() 'drop down event
        Dim tPOINT As POINTAPI
        Dim iIndex As Long
    
        'get the Mouse Cursor Position
        Call GetCursorPos(tPOINT)
        'Convert the Coords to be Relative to the Listbox
        Call ScreenToClient(Combo1.hWnd, tPOINT)
        'Find which Item the Mouse is Over
        iIndex = SendMessage(Combo1.hWnd, LB_ITEMFROMPOINT, 0&, ByVal ((tPOINT.X And &HFF) Or (&H10000 * (tPOINT.Y And &HFF))))
        If iIndex >= 0 Then
            'Extract the List Index
            iIndex = iIndex And &HFF
            'set the Lists ToolTipText
            Combo1.ToolTipText = Combo1.List(iIndex)
        End If
    
    End Sub
    
    Private Sub Form_Load()
        Dim iIndex As Integer
        'Fill the List with Dummy Values
        For iIndex = 1 To 100
            Combo1.AddItem "Item " & iIndex
        Next
        ComboBoxOpenList Combo1, True 'automatically open dropdown list of combo
    End Sub
    The problem here is that it will ONLY show the tooltips when the list isn't dropped anymore.

  13. #13
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Tooltips for each ListBox Item in visual basic 6

    Another old thread dug up.

    Question was asked 10 years ago. I think it safe to say that the questioner is no longer looking for the answer.

    You really should look at the date on the thread and let those which have been dead for years stay dead.
    Always use [code][/code] tags when posting code.

  14. #14
    Join Date
    Jan 2017
    Posts
    10

    Re: Tooltips for each ListBox Item in visual basic 6

    What you are forgetting is the original poster is NOT the only one interested in the information. People show up from Google Searches looking for this information?? Yes?? No?? So until this website removes the page altogether the post is never dead

    Quote Originally Posted by DataMiser View Post
    Another old thread dug up.

    Question was asked 10 years ago. I think it safe to say that the questioner is no longer looking for the answer.

    You really should look at the date on the thread and let those which have been dead for years stay dead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured