Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ComboBox: Change # of items displayed? 1

Status
Not open for further replies.

TimmyNF

Programmer
Aug 26, 2002
11
US
Hi!

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.

Thanks in Advance,

Tim
 
Do you want to change it at design time, or are you looking at dynamically changing during runtime?
 
I wanted to change it at design time with code.

-Tim
 
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

SetComboBoxHeight Me, Combo1, 20
End Sub


 
It works! The only problem in code was the bRepaint parameter in the MoveWindow function. I had to replace the "t" with "True".

Thanks Again!

Tim
 
Sorry, my bad....

Had trouble pasting into here tried retyping that line and apparently got distracted around the end of the line :)
 
Ok, I'll let it slide this time, but just don't let it happen again. :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top