Is there a way I can increase the number of itmes listed in a dropdown combobox? Currently only 8 itmes are listed then the combox is dropped. I wanted at least 15-20 items to appear.
Those two are mutually exclusive... You don't run code at design time... I don't know of a way to modify the standard combobox size at design time in VB (you could create your own user control and have it implement it though.
Here's code to do it at runtime, or could be in the user control if you build your own custom combobox. As a note though, you can't make it any smaller than 8, bigger yes, smaller no.
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 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 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 Const CB_GETITEMHEIGHT = &H154
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type POINTAPI
x As Long
y As Long
End Type
Sub SetComboBoxHeight(frm As Form, cb As ComboBox, nItems As Long)
Dim pt As POINTAPI
Dim rectCB As RECT
Dim nWidth As Long
Dim nItemHeight As Long
Dim nSelHeight As Long
Dim nNewHeight As Long
'Get height/width of combobox
nItemHeight = SendMessageLong(cb.hwnd, CB_GETITEMHEIGHT, 0, 0)
nSelHeight = SendMessageLong(cb.hwnd, CB_GETITEMHEIGHT, -1, 0)
nWidth = cb.Width / Screen.TwipsPerPixelX
'New height requested (# items + room for selection box)
nNewHeight = (nItemHeight * nItems) + (2 * nSelHeight)
'Get Top/Left point of combobox
GetWindowRect cb.hwnd, rectCB
pt.x = rectCB.Left
pt.y = rectCB.Top
'Move combo to new dimensions
ScreenToClient frm.hwnd, pt
MoveWindow cb.hwnd, pt.x, pt.y, nWidth, nNewHeight, t
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 30
Combo1.AddItem "Item #" & i
Next
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.