Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a ComboBox's dropdown area wide enough for its choices
KeywordsComboBox, ComboBox width
CategoriesControls
 
Loop through the ComboBox's choices using TextWidth to see how wide each is. Use SendMessage with the CB_SETDROPPEDWIDTH parameter to make the dropdown area big enough to display the longest choice.
 
Private Sub Form_Load()
Dim i As Integer
Dim max_wid As Long

    ' See how wide the widest ComboBox entry is.
    Me.ScaleMode = vbPixels
    For i = 0 To Combo1.ListCount - 1
        If max_wid < TextWidth(Combo1.List(i)) Then
            max_wid = TextWidth(Combo1.List(i))
        End If
    Next i

    ' Set the width for the dropdown list, adding a margin.
    SendMessage Combo1.hwnd, _
        CB_SETDROPPEDWIDTH, max_wid + 10, 0

    ' Select the first choice.
    Combo1.ListIndex = 0
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated