Results 1 to 7 of 7

Thread: combo box.

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    combo box.

    This should be a good one.

    In Access you are able to make the drop down list wider than the combo box itself to allow to show wider listings.

    Can I do the same in vb. Some API call?

    Any ideas?

  2. #2
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    You can just set the columnwidths if you have multiple columns and the listwidth in design view. There should be properties for both there. That is IF you are using the Microsoft Forms Combo Box (which is what I use).

  3. #3
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    VB Code:
    1. 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
    2. Private Const CB_SETDROPPEDWIDTH = &H160
    3.  
    4. Private Sub Command1_Click()
    5.     'set 500 as the new width
    6.     Call SendMessage (Combo1.hWnd, CB_SETDROPPEDWIDTH, 500, 0)
    7. End Sub

  4. #4

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950
    I'm using the default combo box.

  5. #5

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950
    Thank you very much Serge.

  6. #6

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950
    One more thing,

    Is there a way to position the drop down list on the screen.
    By default it's left property is the same as the combo box's. Is there any way to change that?

  7. #7
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Auto-size combo. Code from www.vbcodelibrary.com

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const CB_SETDROPPEDWIDTH = &H160
    4. Private Const CB_GETDROPPEDWIDTH = &H15F
    5. Private Const DT_CALCRECT = &H400
    6.  
    7. Private Type RECT
    8.    Left As Long
    9.    Top As Long
    10.    Right As Long
    11.    Bottom As Long
    12. End Type
    13.  
    14. Private Declare Function SendMessageLong Lib "user32" Alias _
    15.         "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    16.         ByVal wParam As Long, ByVal lparam As Long) As Long
    17.  
    18. Private Declare Function DrawText Lib "user32" Alias _
    19.     "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, _
    20.     ByVal nCount As Long, lpRect As RECT, ByVal wFormat _
    21.     As Long) As Long
    22.  
    23. Private Sub Command1_Click()
    24. Dim x As Boolean
    25.     x = AutosizeCombo(Combo1)
    26. End Sub
    27.  
    28. Private Sub Form_Load()
    29.  
    30.   Combo1.AddItem "aaaaaaaaa"
    31.   Combo1.AddItem "bbbbbbbbbbbbbbbbbbbbb"
    32.   Combo1.AddItem "ccccc"
    33.   Combo1.AddItem "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
    34.  
    35. End Sub
    36.  
    37. Public Function AutosizeCombo(Combo As ComboBox) As Boolean
    38.    
    39.     'Automatically sizes a combo box to
    40.     'hold the longest item within it
    41.  
    42.     Dim lngRet As Long
    43.     Dim lngCurrentWidth As Single
    44.     Dim rectCboText As RECT
    45.     Dim lngParentHDC As Long
    46.     Dim lngListCount As Long
    47.     Dim lngCounter As Long
    48.     Dim lngTempWidth As Long
    49.     Dim lngWidth As Long
    50.     Dim strSavedFont As String
    51.     Dim sngSavedSize As Single
    52.     Dim blnSavedBold As Boolean
    53.     Dim blnSavedItalic As Boolean
    54.     Dim blnSavedUnderline As Boolean
    55.     Dim blnFontSaved As Boolean
    56.  
    57. On Error GoTo ErrorHandler
    58.  
    59.     'Grab the combo handle and list count
    60.  
    61.     lngParentHDC = Combo.Parent.hdc
    62.     lngListCount = Combo.ListCount
    63.  
    64.     If lngParentHDC = 0 Or lngListCount = 0 Then Exit Function
    65.  
    66.     'Save combo box fonts, etc. to the parent
    67.     'object (form), for testing lengths with the API
    68.  
    69.     With Combo.Parent
    70.  
    71.         strSavedFont = .FontName
    72.         sngSavedSize = .FontSize
    73.         blnSavedBold = .FontBold
    74.         blnSavedItalic = .FontItalic
    75.         blnSavedUnderline = .FontUnderline
    76.        
    77.         .FontName = Combo.FontName
    78.         .FontSize = Combo.FontSize
    79.         .FontBold = Combo.FontBold
    80.         .FontItalic = Combo.FontItalic
    81.         .FontUnderline = Combo.FontItalic
    82.  
    83.     End With
    84.  
    85.     blnFontSaved = True
    86.  
    87.     'Get the width of the widest item
    88.  
    89.     For lngCounter = 0 To lngListCount
    90.        DrawText lngParentHDC, Combo.List(lngCounter), -1, rectCboText, _
    91.             DT_CALCRECT
    92.  
    93.         'Add twenty to the the number as a margin
    94.        lngTempWidth = rectCboText.Right - rectCboText.Left + 20
    95.  
    96.         If (lngTempWidth > lngWidth) Then
    97.            lngWidth = lngTempWidth
    98.         End If
    99.  
    100.     Next
    101.  
    102.     'Get current width of combo
    103.  
    104.     lngCurrentWidth = SendMessageLong(Combo.hwnd, CB_GETDROPPEDWIDTH, _
    105.         0, 0)
    106.  
    107.     'If big enough then that's all A-OK
    108.     If lngCurrentWidth > lngWidth Then
    109.  
    110.         AutosizeCombo = True
    111.         GoTo ErrorHandler
    112.         Exit Function
    113.    
    114.     End If
    115.  
    116.     '... but if not big enough, first calculate
    117.     'the screen width to ensure we don't exceed it!
    118.  
    119.     If lngWidth > Screen.Width \ Screen.TwipsPerPixelX - 20 Then _
    120.         lngWidth = Screen.Width \ Screen.TwipsPerPixelX - 20
    121.  
    122.     'Set the width of our combo
    123.     lngRet = SendMessageLong(Combo.hwnd, CB_SETDROPPEDWIDTH, lngWidth, 0)
    124.  
    125.     'Set the function to True/False depending on API success
    126.     AutosizeCombo = lngRet > 0
    127.    
    128. ErrorHandler:
    129.        
    130.     'If anything goes wrong, revert back!
    131.    
    132.     On Error Resume Next
    133.    
    134.     If blnFontSaved Then
    135.       With Combo.Parent
    136.         .FontName = strSavedFont
    137.         .FontSize = sngSavedSize
    138.         .FontUnderline = blnSavedUnderline
    139.         .FontBold = blnSavedBold
    140.         .FontItalic = blnSavedItalic
    141.      End With
    142.     End If
    143.  
    144. End Function

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