CodeItBetter Programming Another VB Programming Blog

How to expand the width of a combobox to the width of the listed items

Posted on January 5, 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
'Controls - How to expand the width of a combobox to the width of the listed items
Option Explicit
 
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const CB_SETDROPPEDWIDTH = &H160
 
Public Sub ComboWidthAdjustment(Cmb As ComboBox)
    Dim I As Long
    Dim max_wid As Long
    Dim cur_wid As Long
    Dim SaveMode As Long
    Dim Fnt As StdFont
    With Cmb
        SaveMode = .Parent.ScaleMode
        Set Fnt = .Parent.Font
        .Parent.ScaleMode = vbPixels
        Set .Parent.Font = .Font
        For I = 0 To .ListCount - 1
            cur_wid = .Parent.TextWidth(.List(I))
            If max_wid < cur_wid Then max_wid = cur_wid
        Next I
        SendMessage .hWnd, CB_SETDROPPEDWIDTH, max_wid + 10, 0
        .Parent.ScaleMode = SaveMode
        Set .Parent.Font = Fnt
    End With
End Sub

Related posts:

  1. How to Make a ComboBox’s dropdown area wide enough for its choices, allowing for ScaleMode
  2. How to Make a ComboBox’s dropdown area wide enough for its choices
  3. How to set the ComboBox DropDown List Width
  4. How to set the ComboBox DropDown List Height
  5. How to add CheckBox to ComboBox
  6. How to show the Drop Down of ComboBox List
  7. How to get the Width of the String
  8. How to generate list in combobox with all dates of current month
  9. How to Draw 3D Gradient text on form
  10. How to add Horizonal Scroll Bar to ListBox

Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.