How to Make a ComboBox’s dropdown area wide enough for its choices
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 'Controls - How to Make a ComboBox's dropdown area wide enough for its choices '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 |