Results 1 to 3 of 3

Thread: Horizontal scroll bar for combobox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Posts
    20

    Talking Horizontal scroll bar for combobox

    By default a combobox does not have horizontal scroll
    bar when text contained in it is longer than the width
    of the combobox. I( have researched and found that the send message api
    function passing the constant
    CB_SETHORIZONTALEXTEND is suposed to insert a scroll bar if the length of the longest string is longer than the width of the combobox. I doesnt seem to work. Does anybody have any information on how to set a horizontal scrollbar for a combobox.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    I don't know about a horizontal scrollbar, but you can use this to have the combo box expand to the length of its longest entry.
    VB Code:
    1. Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lparam As Long) As Long
    2. Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
    3.  
    4. Private Const CB_SETDROPPEDWIDTH = &H160
    5. Private Const CB_GETDROPPEDWIDTH = &H15F
    6. Private Const DT_CALCRECT = &H400
    7.  
    8. Private Type RECT
    9.    Left As Long
    10.    Top As Long
    11.    Right As Long
    12.    Bottom As Long
    13. End Type
    14.  
    15. Private Function AutosizeCombo(CB As ComboBox) As Boolean
    16.  
    17. 'Automatically sizes a combo box to the longest item within it
    18.  
    19.     Dim LongReturn As Long
    20.     Dim CurrentCBWidth As Single
    21.     Dim TheCBItems As RECT
    22.     Dim ParentHdc As Long
    23.     Dim MyListCount As Long
    24.     Dim MyLongCounter As Long
    25.     Dim TempCBWidth As Long
    26.     Dim LongWidth As Long
    27.     Dim SavedFont As String
    28.     Dim SavedSize As Single
    29.     Dim SavedBold As Boolean
    30.     Dim SavedItalic As Boolean
    31.     Dim SavedUnderline As Boolean
    32.     Dim IsFontSaved As Boolean
    33.  
    34. On Error GoTo ErrorHandler
    35.  
    36.     'Grab the combo handle and list count
    37.     ParentHdc = CB.Parent.hdc
    38.     MyListCount = CB.ListCount
    39.    
    40.     If ParentHdc = 0 Or MyListCount = 0 Then Exit Function
    41.  
    42.     'Save combo box fonts, etc. to the parent object(form), for testing lengths with the API
    43.     'My personal contribution
    44.     With CB.Parent
    45.         SavedFont = .FontName
    46.         SavedSize = .FontSize
    47.         SavedBold = .FontBold
    48.         SavedItalic = .FontItalic
    49.         SavedUnderline = .FontUnderline
    50.         .FontName = CB.FontName
    51.         .FontSize = CB.FontSize
    52.         .FontBold = CB.FontBold
    53.         .FontItalic = CB.FontItalic
    54.         .FontUnderline = CB.FontUnderline
    55.     End With
    56.  
    57.     IsFontSaved = True
    58.  
    59.     'Get the width of the widest item
    60.     For MyLongCounter = 0 To MyListCount
    61.         DrawText ParentHdc, CB.List(MyLongCounter), -1, TheCBItems, DT_CALCRECT
    62.         'Add twenty to the the number as a margin
    63.         TempCBWidth = TheCBItems.Right - TheCBItems.Left + 20
    64.          If (TempCBWidth > LongWidth) Then
    65.             LongWidth = TempCBWidth
    66.          End If
    67.     Next
    68.  
    69.     'Get current width of combo
    70.     CurrentCBWidth = SendMessageLong(CB.hwnd, CB_GETDROPPEDWIDTH, 0, 0)
    71.  
    72.     'If big enough then that's all A-OK
    73.     If CurrentCBWidth > LongWidth Then
    74.         AutosizeCombo = True
    75.         GoTo ErrorHandler
    76.         Exit Function
    77.     End If
    78.  
    79.     '... but if not big enough, first calculate the screen width to ensure we don't exceed it!
    80.     If LongWidth > Screen.Width \ Screen.TwipsPerPixelX - 20 Then _
    81.         LongWidth = Screen.Width \ Screen.TwipsPerPixelX - 20
    82.  
    83.     'Set the width of our combo
    84.     LongReturn = SendMessageLong(CB.hwnd, CB_SETDROPPEDWIDTH, LongWidth, 0)
    85.  
    86.     'Set the function to True/False depending on API success
    87.     AutosizeCombo = LongReturn > 0
    88.    
    89. ErrorHandler:
    90.        
    91.     'If anything blows up, reset the combo to its original state
    92.     On Error Resume Next
    93.    
    94.     If IsFontSaved Then
    95.       With CB.Parent
    96.         .FontName = SavedFont
    97.         .FontSize = SavedSize
    98.         .FontUnderline = SavedUnderline
    99.         .FontBold = SavedBold
    100.         .FontItalic = SavedItalic
    101.      End With
    102.    End If
    103.  
    104. End Function
    105.  
    106. 'Usage:  In the dropdown even of the combo box, place this code
    107. 'Dim x
    108. 'x = AutosizeCombo(Combo1)

  3. #3
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    I haven't tested it, but it may work

    VB Code:
    1. Private Declare Function ShowScrollBar Lib "user32" Alias "ShowScrollBar" (ByVal hwnd As Long, ByVal wBar As Long, ByVal bShow As Long) As Long
    2. Private Const SB_BOTH = 3
    3. Private Const SB_HORZ = 0
    4. Private Const SB_VERT = 1
    5. ShowScrollBar Combo1.hwnd, SB_BOTH, 1

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