How to Make a ComboBox’s dropdown area wide enough for its choices, allowing for ScaleMode
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 24 25 26 27 28 29 30 31 32 | 'Controls - How to Make a ComboBox's dropdown area wide enough for its choices, allowing for ScaleMode 'Use the SendMessage API function to send the ComboBox the CB_SETDROPPEDWIDTH 'message, specifying the desired width in pixels. This version saves the 'ComboBox's parent's ScaleMode value so it can switch to pixels if necessary. Public Sub ComboWidthAdjustment(Cmb As ComboBox) 'SaveMode added to allow greater safety in case other parts of code expect different ScaleModes 'cur_wid added to speed up processing 'Uses Parent so that only the Combo needs to be passed to procedure Dim I As Long Dim max_wid As Long Dim cur_wid As Long Dim SaveMode As Long With Cmb 'Save Fom's ScaleMode SaveMode = .Parent.ScaleMode 'Reset to vbPixels for TextWidth to work properly .Parent.ScaleMode = vbPixels 'Find longest member For I = 0 To .ListCount - 1 cur_wid = .Parent.TextWidth(.List(I)) If max_wid < cur_wid Then max_wid = cur_wid End If Next I ' Set the width for the dropdown list, adding a margin. SendMessage .hWnd, CB_SETDROPPEDWIDTH, max_wid + 10, 0 'restore form's ScaleMode .Parent.ScaleMode = SaveMode End With End Sub |