Visual Basic Combo API
SendMessage: Change Combo Box List Width
     
Posted:   Friday July 10, 1998
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB5, Windows 98
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

GetTextExtentPoint32: Change Combo List Width Based on Contents
       
 Prerequisites
None.

This routine demonstrates using the API to change the width of the dropdown portion of a combo box. The combo list width is manually set in the accompanying text box.

Start a new project, and on the form place a combo box (Combo1), a text box (Text1), a command button (Command1) and two labels. Name the label over the combo box Label1, and add the label caption for the textbox Label2. The list shouldn't be too wide to start. Set the text property of the textbox to 228.

In the form's load event, add code to load several combo list items of varying length, making sure that some are wider than the control's width.

 BAS Module Code
None.

 Form Code
To the form, add the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private 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_GETLBTEXTLEN = &H149
Private Const CB_SHOWDROPDOWN = &H14F
Private Const CB_GETDROPPEDWIDTH = &H15F
Private Const CB_SETDROPPEDWIDTH = &H160



Private Sub Command1_Click()

   Dim cwidth As Long
   Dim NewDropDownWidth As Long
   
  'check if a number is entered into Text1. 
  'If not, bail out.
   If Val(Text1.Text) Then
     'here we simply set the dropdown list size to 
     'the value entered in Text1. Note: If the proposed 
     'width this is less than the width of the combo
     'portion, the combo width is used (the dropdown 
     'can never be narrower than the combo box)
      NewDropDownWidth = Val(Text1.Text)
   
     'resize the dropdown portion of the combo box using SendMessage
      Call SendMessage(Combo1.hwnd, CB_SETDROPPEDWIDTH, NewDropDownWidth, ByVal 0)
   
     'reflect the new dropdown list width in the Label   
      cwidth = SendMessage(Combo1.hwnd, CB_GETDROPPEDWIDTH, 0, ByVal 0)
      Label1.Caption = "Current dropdown width = " & cwidth & " pixels."
   
     'drop the list down by code to show the new size   
      Call SendMessage(Combo1.hwnd, CB_SHOWDROPDOWN, True, ByVal 0)
      
   End If

End Sub
 Comments
You can experiment setting different widths. If you didn't resize the default combo box, I suggest starting with 230-300 as the NewDropDownWidth number. Note that the unit of measurement used for resizing the combo box is 'dialog units', and so will not easily correspond to any ScaleMode setting you might set on your form.

This routine is resolution-independent. Any new width will produce the exact same results on 640x480 and 1024x768, that is, if it sizes the dropdown perfectly at 640, it will size it perfectly at 1024.

If your application uses a combo box to display data that you provide (as oppose to load at runtime from the users entry), and you therefore always know what the length of the longest item will be, you can use this method to hard-code the appropriate value as NewDropDownWidth.

If you require a dynamic approach, see GetTextExtentPoint32: Change Combo List Width Based on Contents.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter