.NET Programming With Me

VB.NET : Adjusting ComboBox DropDownList width to longest string width

A ComboBox dropdown width defaults to the width of the control itself. When items in the list are longer than the control, they get clipped and the user cannot read them without selecting. Windows Forms does not resize the dropdown automatically, so you have to measure and set it yourself.

This comes up any time a ComboBox is sized to fit a form layout rather than its content, which is most of the time. A narrow control with long display values is one of those small usability issues that is easy to overlook and just as easy to fix.

The fix

The function below measures every item string using the control's own font and Graphics context, accounts for the vertical scrollbar width when the item count exceeds MaxDropDownItems, and sets DropDownWidth to the widest measured value.

Public Shared Function AdjustComboBoxWidth(ByVal sender As Object, ByVal e As EventArgs)
    Dim senderComboBox  = DirectCast(sender, ComboBox)
    Dim width           As Integer = senderComboBox.DropDownWidth
    Dim g               As Graphics = senderComboBox.CreateGraphics()
    Dim font            As Font = senderComboBox.Font

    Dim vertScrollBarWidth As Integer = If(
        (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems),
        SystemInformation.VerticalScrollBarWidth,
        0)

    Dim newWidth As Integer
    For Each s As String In DirectCast(sender, ComboBox).Items
        newWidth = CInt(g.MeasureString(s, font).Width) + vertScrollBarWidth
        If width < newWidth Then
            width = newWidth
        End If
    Next

    senderComboBox.DropDownWidth = width
    Return False
End Function

Wire it up to the DropDown event of any ComboBox so it runs each time the list opens. That way it adapts if the items change between openings:

AddHandler comboBox1.DropDown, AddressOf AdjustComboBoxWidth

Because the function signature matches the standard EventHandler delegate (sender As Object, e As EventArgs), the same shared function can be reused across every ComboBox on every form in the project without duplication.

Tip: Graphics.MeasureString can slightly overestimate string width due to GDI character spacing. If you find the dropdown a touch too wide, subtract a small constant (typically 2 to 4 pixels) from newWidth after measuring. Alternatively, use TextRenderer.MeasureText with TextFormatFlags.NoPadding for a tighter and more accurate measurement that matches how Windows Forms actually renders text.

When to use this
  • Any ComboBox whose items are populated at runtime from a database or a dynamic source where the longest value is not known at design time.
  • Forms where the ComboBox width is constrained by the layout and cannot simply be made wider to accommodate all content.
  • Shared utility libraries where a single reusable handler can be attached to all ComboBox controls across a large Windows Forms application.
Found this useful? Drop a comment below if you have a variation or ran into an edge case I did not cover.

No comments: