Here's some code a friend of mine wrote for autosizing the combo box to it's contents.
Code
Public Sub ComboDropDownWidth(ByRef cboThis As ComboBox, _
ByVal lWidth As Long)
'PS 11/05/06 PUK5023 Added to automatically size a combo drop-down width to the widest entry
'PS 11/05/06 PUK5023 This function will custom re-size a combo drop-down width (see also ComboDropDownWidthFromContents)
On Error GoTo ComboDropDownWidth_Err
SendMessageLong cboThis.hWnd, CB_SETDROPPEDWIDTH, lWidth, 0
Exit_Point:
Exit Sub
'' Error Handling
ComboDropDownWidth_Err:
LogError "modNYFixLibrary.ComboDropDownWidth", Err.Number, Err.Description
GoTo Exit_Point
Resume
End Sub
Public Sub ComboDropDownWidthFromContents(ByRef cboThis As ComboBox, _
Optional ByVal lMaxWidth = -1)
'PS 11/05/06 PUK5023 Added to automatically size a combo drop-down width to the widest entry
Dim i As Long
Dim tR As RECT
Dim lW As Long
Dim lWidth As Long
Dim lHDC As Long
On Error GoTo ComboDropDownWidthFromContents_Err
' Cache the HDC of the parent form for speed:
lHDC = cboThis.Parent.HDC
' Loop through each combo box list item & get its
' width, storing the largest:
For i = 0 To cboThis.ListCount - 1
DrawText lHDC, cboThis.List(i), -1, tR, DT_CALCRECT
lW = tR.Right - tR.Left + 8
If (lW > lWidth) Then
lWidth = lW
End If
Next i
' Don't allow width to exceed specified max
' width, or the width of the screen:
If lMaxWidth <= 0 Then
lMaxWidth = Screen.Width \ Screen.TwipsPerPixelX - 16
End If
If (lWidth > lMaxWidth) Then
lWidth = lMaxWidth
End If
' Combo box looks a bit strange when the
' drop down portion is smaller than the
' combo box itself:
If (lWidth < cboThis.Width \ Screen.TwipsPerPixelX) Then
lWidth = cboThis.Width \ Screen.TwipsPerPixelX
ComboDropDownWidth cboThis, lWidth
Else
'If it is longer Set the drop down width and add a little for good measure otherwise it still obscures the last character
ComboDropDownWidth cboThis, lWidth + 20
End If
Exit_Point:
Exit Sub
'' Error Handling
ComboDropDownWidthFromContents_Err:
LogError "modNYFixLibrary.ComboDropDownWidthFromContents", Err.Number, Err.Description
GoTo Exit_Point
Resume
End Sub