The new vbAccelerator Site - more VB and .NET Code and Controls

Change the drop-down width of a combo box

Author:

Steve McMahon(steve@vbaccelerator.com)

Keywords:

API,Combo Box,Windows Controls

Updated:

12/10/98

Other Tips
All Tips
By Date
By Subject


API (33)
Bit
Manipulation (3)

Clipboard (3)
Combo
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Interprocess
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Text
Box (2)

Windows (11)
Windows
Controls (10)



Submit


This tip shows you how to get and set the width of the drop down portion of a combo box. It also includes code to automatically set the drop down width based on the contents of a combo box by measuring the size of the text in each combo box item.

Start a new project and add a module. Then add the following code to the module:

' These functions required to set the drop-down width:
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const CB_GETDROPPEDWIDTH = &H15F
Private Const CB_SETDROPPEDWIDTH = &H160

' These are only required if you want to automatically
' calculate the drop-down width:
Private Type RECT
&nbsp &nbsp Left As Long
&nbsp &nbsp Top As Long
&nbsp &nbsp Right As Long
&nbsp &nbsp Bottom As Long
End Type
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Private Const DT_CALCRECT = &H400

Public Property Let DropDownWidth(ByRef cboThis As ComboBox, ByVal lWidth As Long)
&nbsp &nbsp SendMessageLong cboThis.hwnd, CB_SETDROPPEDWIDTH, lWidth, 0
End Property
Public Property Get DropDownWidth(ByRef cboThis As ComboBox) As Long
Dim lW As Long
&nbsp &nbsp DropDownWidth = SendMessageLong(cboThis.hwnd, CB_GETDROPPEDWIDTH, 0, 0)
End Property
Public Sub DropDownWidthFromContents(ByRef cboThis As ComboBox, Optional ByVal lMaxWidth = -1)
Dim i As Long
Dim tR As RECT
Dim lW As Long
Dim lWidth As Long
Dim lHDC As Long

&nbsp &nbsp
' Evaluate the width of each item in the
&nbsp &nbsp ' combo box:
&nbsp &nbsp
&nbsp &nbsp
' First set the combo's parent form font to the
&nbsp &nbsp ' combo font:
&nbsp &nbsp With cboThis.Parent.Font
&nbsp &nbsp &nbsp &nbsp .Name = cboThis.Font.Name
&nbsp &nbsp &nbsp &nbsp .Size = cboThis.Font.Size
&nbsp &nbsp &nbsp &nbsp .Bold = cboThis.Font.Bold
&nbsp &nbsp &nbsp &nbsp
' Surely you don't have a combo box with
&nbsp &nbsp &nbsp &nbsp ' italic font?
&nbsp &nbsp &nbsp &nbsp .Italic = cboThis.Font.Italic
&nbsp &nbsp End With
&nbsp &nbsp
' Cache the HDC of the parent form for speed:
&nbsp &nbsp lHDC = cboThis.Parent.hdc
&nbsp &nbsp
&nbsp &nbsp
' Loop through each combo box list item & get its
&nbsp &nbsp ' width, storing the largest:
&nbsp &nbsp For i = 0 To cboThis.ListCount - 1
&nbsp &nbsp &nbsp &nbsp DrawText lHDC, cboThis.List(i), -1, tR, DT_CALCRECT
&nbsp &nbsp &nbsp &nbsp lW = tR.Right - tR.Left + 8
&nbsp &nbsp &nbsp &nbsp If (lW > lWidth) Then
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lWidth = lW
&nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp Next i
&nbsp &nbsp
&nbsp &nbsp
' Don't allow width to exceed specified max
&nbsp &nbsp ' width, or the width of the screen:
&nbsp &nbsp If lMaxWidth &nbsp &nbsp &nbsp &nbsp lMaxWidth = Screen.Width \ Screen.TwipsPerPixelX - 16
&nbsp &nbsp End If
&nbsp &nbsp If (lWidth > lMaxWidth) Then
&nbsp &nbsp &nbsp &nbsp lWidth = lMaxWidth
&nbsp &nbsp End If
&nbsp &nbsp
&nbsp &nbsp
' Combo box looks a bit strange when the
&nbsp &nbsp ' drop down portion is smaller than the
&nbsp &nbsp ' combo box itself:
&nbsp &nbsp If (lWidth &nbsp &nbsp &nbsp &nbsp lWidth = cboThis.Width \ Screen.TwipsPerPixelX
&nbsp &nbsp End If
&nbsp &nbsp
&nbsp &nbsp
' Set the drop down width:
&nbsp &nbsp DropDownWidth(cboThis) = lWidth
End Sub

To try out a the function, add a Combo box, a Label and three Command buttons to your project's form. Set the captions for the command buttons as follows: &nbsp &nbsp Command1 &nbsp &nbsp A&dd String
&nbsp &nbsp Command2 &nbsp &nbsp &Calc Width
&nbsp &nbsp Command3 &nbsp &nbsp &Set Width...
Then add this code to the form:

Private Sub Command1_Click()
Dim sI As String
&nbsp &nbsp sI = InputBox("Enter string", , "New item")
&nbsp &nbsp If (sI "") Then
&nbsp &nbsp &nbsp &nbsp Combo1.AddItem sI
&nbsp &nbsp End If
End Sub

Private Sub Command2_Click()
&nbsp &nbsp DropDownWidthFromContents Combo1
&nbsp &nbsp Label1.Caption = DropDownWidth(Combo1)
End Sub

Private Sub Command3_Click()
Dim sI As String
&nbsp &nbsp sI = InputBox("Enter width", , DropDownWidth(Combo1))
&nbsp &nbsp If IsNumeric(sI) Then
&nbsp &nbsp &nbsp &nbsp DropDownWidth(Combo1) = CLng(sI)
&nbsp &nbsp &nbsp &nbsp Label1.Caption = DropDownWidth(Combo1)
&nbsp &nbsp End If
End Sub

Private Sub Form_Load()
&nbsp &nbsp Label1.Caption = DropDownWidth(Combo1)
End Sub

Start the project. The width of the combo box wil lbe added to the label control. You can use the Add String button to add new items to the combo box, Calc Width to automatically set the drop down width to the control's contents and Set Width to set your own width. Note that all widths are specified in pixels.


&nbsp

Related Tips and Articles:

&nbsp

AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 12/10/98