Results 1 to 6 of 6

Thread: Combobox Tooltips

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    31

    Combobox Tooltips

    I was just wondering, is there any way to display tooltips for combobox entry's that are to long to read? Or even better is there a way to extend the combobox, but only for the currently highlighted text if it is to long for the combobox.

    It has been done here, but for c++

    http://www.codeproject.com/KB/combob...pcombobox.aspx

    If there's anyway to replicate the third picture on that site in vb6 that would be perfect.
    Last edited by Jackkkkk; Sep 13th, 2010 at 02:42 PM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Combobox Tooltips

    i have an example somewhere that allows the dropdown of the combobox to be wider than the combobox, if that is of any help
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Combobox Tooltips

    Try this..
    vb Code:
    1. Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" _
    2. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    3. ByVal lparam As Long) As Long
    4.  
    5. Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, _
    6. ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, _
    7. ByVal wFormat As Long) As Long
    8.  
    9. Private Const CB_SETDROPPEDWIDTH = &H160
    10. Private Const CB_GETDROPPEDWIDTH = &H15F
    11. Private Const DT_CALCRECT = &H400
    12.  
    13. Private Type RECT
    14.    Left As Long
    15.    Top As Long
    16.    Right As Long
    17.    Bottom As Long
    18. End Type
    19.  
    20. Private Function AutosizeCombo(CB As ComboBox) As Boolean
    21. 'Automatically sizes a combo box to hold the longest item within it
    22. 'A substantial portion of this code was written by other people. I found
    23. 'the basis for this on one of the 'VB Source Code Web sites.   Originally,
    24. 'this routine was passed a number and the routine resized the combo box
    25. 'to the width of the passed number, not to the size of its 'longest item width.    
    26. 'In another routine, I found an example of useing the DrawText API and thought
    27. 'it would be a cool idea to combine the two into a routine which does what this does.
    28. 'The only truly original aspect of this routine is the feature that I built in to
    29. 'accommodate changes in font size and style (bold,  italic, underline, name).  
    30. 'The rest is an congolmoration of the two pieces of code already mentioned.
    31.  
    32.     Dim LongReturn As Long
    33.     Dim CurrentCBWidth As Single
    34.     Dim TheCBItems As RECT
    35.     Dim ParentHdc As Long
    36.     Dim MyListCount As Long
    37.     Dim MyLongCounter As Long
    38.     Dim TempCBWidth As Long
    39.     Dim LongWidth As Long
    40.     Dim SavedFont As String
    41.     Dim SavedSize As Single
    42.     Dim SavedBold As Boolean
    43.     Dim SavedItalic As Boolean
    44.     Dim SavedUnderline As Boolean
    45.     Dim IsFontSaved As Boolean
    46.  
    47. On Error GoTo ErrorHandler
    48.  
    49.     'Grab the combo handle and list count
    50.     ParentHdc = CB.Parent.hdc
    51.     MyListCount = CB.ListCount
    52.    
    53.     If ParentHdc = 0 Or MyListCount = 0 Then Exit Function
    54.  
    55.     'Save combo box fonts, etc. to the parent object(form),
    56.     'for testing lengths with the API
    57.     'My personal contribution
    58.     With CB.Parent
    59.         SavedFont = .FontName
    60.         SavedSize = .FontSize
    61.         SavedBold = .FontBold
    62.         SavedItalic = .FontItalic
    63.         SavedUnderline = .FontUnderline
    64.         .FontName = CB.FontName
    65.         .FontSize = CB.FontSize
    66.         .FontBold = CB.FontBold
    67.         .FontItalic = CB.FontItalic
    68.         .FontUnderline = CB.FontUnderline
    69.     End With
    70.  
    71.     IsFontSaved = True
    72.  
    73.     'Get the width of the widest item
    74.     For MyLongCounter = 0 To MyListCount
    75.         DrawText ParentHdc, CB.List(MyLongCounter), -1, TheCBItems, DT_CALCRECT
    76.         'Add twenty to the the number as a margin
    77.         TempCBWidth = TheCBItems.Right - TheCBItems.Left + 20
    78.          If (TempCBWidth > LongWidth) Then
    79.             LongWidth = TempCBWidth
    80.          End If
    81.     Next
    82.  
    83.     'Get current width of combo
    84.     CurrentCBWidth = SendMessageLong(CB.hwnd, CB_GETDROPPEDWIDTH, 0, 0)
    85.  
    86.     'If big enough then that's all A-OK
    87.     If CurrentCBWidth > LongWidth Then
    88.         AutosizeCombo = True
    89.         GoTo ErrorHandler
    90.         Exit Function
    91.     End If
    92.  
    93.     '... but if not big enough, first calculate the screen width to ensure
    94.     'we don't exceed it!
    95.     If LongWidth > Screen.Width \ Screen.TwipsPerPixelX - 20 Then _
    96.         LongWidth = Screen.Width \ Screen.TwipsPerPixelX - 20
    97.  
    98.     'Set the width of our combo
    99.     LongReturn = SendMessageLong(CB.hwnd, CB_SETDROPPEDWIDTH, LongWidth, 0)
    100.  
    101.     'Set the function to True/False depending on API success
    102.     AutosizeCombo = LongReturn > 0
    103.    
    104. ErrorHandler:
    105.        
    106.     'If anything blows up, reset the combo to its original state
    107.     On Error Resume Next
    108.    
    109.     If IsFontSaved Then
    110.       With CB.Parent
    111.         .FontName = SavedFont
    112.         .FontSize = SavedSize
    113.         .FontUnderline = SavedUnderline
    114.         .FontBold = SavedBold
    115.         .FontItalic = SavedItalic
    116.      End With
    117.    End If
    118.  
    119. End Function
    120.  
    121. 'In the dropdown even of the combo box, place this code
    122. Private Sub Combo1_DropDown()
    123. Dim x As Variant
    124. x = AutosizeCombo(Combo1)
    125. End Sub
    Last edited by Hack; Sep 13th, 2010 at 12:31 PM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    31

    Re: Combobox Tooltips

    Hmm, that doesn't seem to be working, it only displays a tooltip for the item that is selected on the mouse over event, instead of when a item is highlighted.
    Last edited by Jackkkkk; Sep 13th, 2010 at 02:44 PM.

  6. #6

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