Visual Basic Combo API
MoveWindow: Change Combo Box List Height
     
Posted:   Friday July 24, 1998
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6, and VB3, VB4-16 with appropriate declarations (untested)
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   Karl E. Peterson, VBnet - Randy Birch
     
 Prerequisites
None. Note however this does not work against the data combo control.

The Visual Basic combo box dropdown - unlike its C counterpart - is limited to displaying only eight items. This page shows how to change the dropdown height to any number greater than eight.

As often is the case, credit must be extended to others for providing some of the framework used here. This time the kudos go to Karl E. Peterson for his adjustment routine.

 BAS Module Code
None.

 Form Code
Add a combo box (Combo1) a label (Label1) and a command button (Command1) to a form, and add 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 Const CB_SHOWDROPDOWN = &H14F
Private Const CB_GETITEMHEIGHT = &H154
         
Private Type POINTAPI
   x As Long
   y As Long
End Type

Private Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type

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 Declare Function MoveWindow Lib "user32" _
  (ByVal hWnd As Long, _
   ByVal x As Long, ByVal y As Long, _
   ByVal nWidth As Long, _
   ByVal nHeight As Long, _
   ByVal bRepaint As Long) As Long

Private Declare Function GetWindowRect Lib "user32" _
  (ByVal hWnd As Long, _
   lpRect As RECT) As Long

Private Declare Function ScreenToClient Lib "user32" _
  (ByVal hWnd As Long, _
   lpPoint As POINTAPI) As Long



Private Sub Form_Load()

   Dim i As Integer
   
  'add a few strings to the combo
   For i = 1 To 50
   
      Combo1.AddItem CStr(i) & " - combo box string number "
   
   Next
   
End Sub


Private Sub Command1_Click()

   Dim pt As POINTAPI
   Dim rc As RECT
   Dim cWidth As Long
   Dim newHeight As Long
   Dim oldScaleMode As Long
   Dim numItemsToDisplay As Long
   Dim itemHeight As Long
   
  'how many items should appear in the dropdown?
   numItemsToDisplay = 16
   Label1.Caption = "Items displayed = " & numItemsToDisplay

  'Save the current form scalemode, then
  'switch to pixels
   oldScaleMode = Form1.ScaleMode
   Form1.ScaleMode = vbPixels
   
  'the width of the combo, used below
   cWidth = Combo1.Width
  
  'get the system height of a single
  'combo box list item
   itemHeight = SendMessage(Combo1.hWnd, CB_GETITEMHEIGHT, 0, ByVal 0)
   
  'Calculate the new height of the combo box. This
  'is the number of items times the item height
  'plus two. The 'plus two' is required to allow
  'the calculations to take into account the size
  'of the edit portion of the combo as it relates
  'to item height. In other words, even if the
  'combo is only 21 px high (315 twips), if the
  'item height is 13 px per item (as it is with
  'small fonts), we need to use two items to
  'achieve this height.
   newHeight = itemHeight * (numItemsToDisplay + 2)
   
  'get the co-ordinates of the combo box
  'relative to the screen
   Call GetWindowRect(Combo1.hWnd, rc)
   pt.x = rc.Left
   pt.y = rc.Top

  'then translate into co-ordinates
  'relative to the form.
   Call ScreenToClient(Form1.hWnd, pt)

  'using the values returned and set above,
  'call MoveWindow to reposition the combo box
   Call MoveWindow(Combo1.hWnd, pt.x, pt.y, Combo1.Width, newHeight, True)
   
  'it's done, so show the new combo height
   Call SendMessage(Combo1.hWnd, CB_SHOWDROPDOWN, True, ByVal 0)
   
  'restore the original form scalemode
  'before leaving
   Form1.ScaleMode = oldScaleMode
   
End Sub
 Comments
This routine has been tested under all versions of Windows except Windows 2003 (so far).

The system on which this code was tested was running small fonts; there may be a tweak needed in the calculations should large fonts affect a different outcome.


 
 

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