logo
vbRad Home
Source Code
Book Reviews
Forum
Links
About Us
Contribute

Compare Databases with SQL Effects Clarity
 
 How to AutoSize the DropDown portion of a ComboBox

Posted on
2/18/2001
Author:
Robert Gelb
Email:
Not Shown
Applies To OS:
NT, 9x, 2000
Product:
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




Add Your Comment  

Name: Email Address: all fields optional
Notify me via email when someone responds to this message (valid email required).

Enter the word:
 



Comments
#1. By Matt. Posted on 6/11/2006 4:30:49 AM
Excellent! I've been looking for some code to expand the length of the combo box and this should do the trick!!!

Matt.

#2. By manoj. Posted on 7/19/2006 4:22:11 AM
Its cool after long time i have created a ocx using the same component.Thanx a lot for uploading such code on the net..