SetDroppedWidth




Private Sub Command10_Click()
Dim r As Long
Dim sSelected As String
Dim rc As RECT
Dim avgWidth As Single
Dim newWidth As Long
Dim i As Long
Dim NumOfChars As Long
Dim LongestSoFar As Integer
Dim LongestIndex As Long
'----------------------------------------------------------

'loop through the combo entries, using SendMessage

'with CB_GETLBTEXTLEN to determine the longest item

'in the dropdown portion of the combo

For i = 0 To Combo1.ListCount - 1
NumOfChars = SendMessage(Combo1.hwnd, CB_GETLBTEXTLEN, i, 0)
If NumOfChars > LongestSoFar Then
LongestSoFar = NumOfChars
LongestIndex = i
End If
Next
'get the length of the longest combo item

'and calc its average width. Add .5 to round up

sSelected = (Combo1.List(LongestIndex))
r = DrawText(Form1.hDC, sSelected, -1&, rc, DT_CALCRECT)
avgWidth = rc.Right / Len(sSelected)
'calc the combo's new width by multiplying the

'average * number of characters, and adding the

'system metrics for a scrollbar

newWidth = (Len(sSelected) * avgWidth) + (cScrollWidth * 2)
'resize the dropdown portion of the combo box

r = SendMessage(Combo1.hwnd, CB_SETDROPPEDWIDTH, newWidth, 0)
'reflect the new dropdown list width in Label4

r = SendMessage(Combo1.hwnd, CB_GETDROPPEDWIDTH, 0, 0)
End Sub











( setdroppedwidth.html )- by Paolo Puglisi - Modifica del 17/12/2023