How to AutoSize the DropDown portion of a ComboBox

Applies To

OS:
VB:
NT, 9x, 2000
5, 6

Often, when using Combo Boxes there is a situation where one item is much longer than others.  Consequently when the users drop down the combo box, they can't see all the entries in its entirety.  Provided for your coding enjoyment is a set of routines that will autosize the DropDown portion of the Combo Box so that the user can see all the entries (unless you have ridiculously long ones).  Take a look at the images below. 

Before Autosize After Autosize

You can either download the project (2 kb) or copy and paste from below

Project Setup instructions
  1. Create a standard Exe project
  2. Place a Combo Box control on the form
  3. Place a Command Button on the form. Set the Caption property to AutoSize
  4. Move the Command Button elsewhere on the form so that the ComboBox is clearly visible
  5. Add a Module to the project
Add the following to Module1

Option Explicit

'general declarations
Private Declare Function SendMessageBynum _
    Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, _
    ByVal lParam As Long) As Long
Private Const CB_SETDROPPEDWIDTH = &H160

'this function sets the width of the list portion of the combo box
Public Sub SetComboBoxDropDownWidth(hWnd As Long, WidthPx As Long)
    'Parameters: hWnd - handle to the ComboBox (gotten through ComboBox.hWnd)
    '            WidthPx - width of the list portion of the combo box in pixels

    SendMessageBynum hWnd, CB_SETDROPPEDWIDTH, WidthPx, 0

End Sub	

'this function autosizes the list portion of the combo box
Public Sub AutoSizeComboBoxDropDown(cmb As ComboBox)
    'Parameters: cmb - ComboBox control/object to perform the Autosize on

    Dim CurrentEntryWidth As Integer
    Dim PixelLength As Long
    Dim x As Integer
    Dim oFormFont As StdFont
    Dim iScaleMode As Integer 'find the longest string in the list portion of the combobox
        
    'temporarily set the form font to the combo box font
    'First cache the font
    Set oFormFont = cmb.Parent.Font
    
    'now set the combo box font to the form font
    Set cmb.Parent.Font = cmb.Font
    
    'temporarily change the ScaleMode of the form to Pixel
    'first cache the ScaleMode
    iScaleMode = cmb.Parent.ScaleMode
    
    'now set the ScaleMode to Pixel
    cmb.Parent.ScaleMode = vbPixels
    
    'find out the length in pixels of the longest string in the combo box
    For x = 0 To cmb.ListCount - 1
        CurrentEntryWidth = cmb.Parent.TextWidth(cmb.List(x))
        If CurrentEntryWidth > PixelLength Then
            PixelLength = CurrentEntryWidth
        End If
    Next
    
    'then add 10 pixels for a good measure (actually, to account for combobox margins)
    SetComboBoxDropDownWidth cmb.hWnd, PixelLength + 10
    
    'reset the ScaleMode to its original value
    cmb.Parent.ScaleMode = iScaleMode
    
    'reset to the original form font
    Set cmb.Parent.Font = oFormFont

End Sub
Add the following code to Form1

Private Sub Form_Load()
    Combo1.AddItem "abcd"
    Combo1.AddItem "abcdef"
    Combo1.AddItem "abcdefg3"
    Combo1.AddItem "abcdefg4234"
    Combo1.AddItem "abcdefg523452"
    Combo1.AddItem "abcdefg67890123456"
    Combo1.AddItem "WWWWWWWWWWWW"
End Sub

Private Sub Command1_Click()
    AutoSizeComboBoxDropDown Combo1
End Sub
Remarks

  1. Run the project
  2. Drop down the ComboBox. Note the width
  3. Click on the AutoSize button
  4. Note the width again. It should match the longest string in the ComboBox