Sizeable ComboBox?!

Is it possible to make ComboBox sizeable? Just like other controls like TextBox, DTPicker, etc.
Currently its size depends on selected font.. Can it be changed through API calls, subclassing?

hpet
LVL 1
hpetAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
TimCotteeConnect With a Mentor Commented:
This code uses the api calls to change the size of the normal combo box.

Paste this into a new module

Option Explicit


' for ENTER keystrokes
Public Const VK_EXECUTE& = &H2B

'from the old days... used to need this
'Public Const WM_USER& = &H400

Public Const CB_GETITEMHEIGHT& = &H154
Public Const CB_SETITEMHEIGHT& = &H153
Public Const CB_GETDROPPEDSTATE& = &H157

Public Const LB_SETITEMHEIGHT& = &H1A0
Public Const LB_GETITEMHEIGHT& = &H1A1


Public Declare Function SendMessageBynum& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long)


In your form use something like:

Dim di&
   
di& = SendMessageBynum(Combo1.hwnd, CB_GETITEMHEIGHT, -1, 0&)

To get the current height of the combo box and:

Dim NewHeight As Long
NewHeight = CLng(Val(txtSetComboEditHeight))
   
If Not NewHeight < 5 Then
On Error GoTo ResizeErr
Dim di&
di& = SendMessageBynum(Combo1.hwnd, CB_SETITEMHEIGHT, -1, NewHeight)
0
 
caraf_gCommented:
There are some other comboboxes available, such as the one that comes with the Forms2 library. But the standard combo box cannot be changed.
0
 
TimCotteeCommented:
PS if you use 0 instead of -1 in the third parameter it will change the line height of the list portion rather than the edit section of the combo box. This will also work for list boxes.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
caraf_gCommented:
Hi TimCottee,

Great code! But setting the 3rd parameter to 0 gave some odd results. The list portion is either not shown or the normal size?
0
 
caraf_gCommented:
Excellent... I just worked out it works for small values i.e. something between 0 and 30? Where 0 makes the list items very small and 30 makes the list items very large. There seems to be a maximum size for the list itself though, after which you just end up seeing less of the items. Any way of changing that?
0
 
TimCotteeCommented:
caraf_g

It works fine for me using
Dim NewHeight As Long
NewHeight = nn
di& = SendMessageBynum(Combo1.hwnd, CB_SETITEMHEIGHT, 0, NewHeight)
0
 
TimCotteeCommented:
Set IntegralHeight to false on the combo box. It won't then try to automatically resize the list portion to whatever it thinks is right even though it has been changed by the back door.
0
 
caraf_gCommented:
IntegralHeight = false doesn't make any difference.

What I'm talking about is the height of the drop down list as a whole, it seems to get set to some maximum value and then doesn't change no matter how large you make each item?
0
 
TimCotteeCommented:
caraf_g I think this is a VB limitation. I have tried using the setwindowrgn api calls to affect the list box portion but don't seem to be able to get any result. It changes the edit section ok but I can't get a handle to the list portion.
0
 
caraf_gCommented:
Not to worry, that was an excellent bit of code there. Hope hpet rewards you accordingly!
0
 
TimCotteeCommented:
Thanks for the vote of confidence! I'll let you know if I ever find a way around it though.
0
 
caraf_gCommented:
Yes, great. Just post another comment here; I'll get the notification.

Thanks

Pino
0
 
hpetAuthor Commented:
Ok guys,

In a mean time I managed to figure out same thing. It is something, but what I really wanted is to make combo sizeable all the time, not just calling API and being stucked again with fixed size.
Well there are some work arounds, but they aren't beautiful.
Forms 2.0, unfortunatly we are not permited to distribute file with our programs ( + controls are made kind of for web development so sometimes they take "strange" actions you don't have control on).

About combobox sizing. Seems like "OnSetExtend" function in ComboBox control class ignores vertical sizing and that it would take some more time to subclass it...
Perhaps I will get to down to it when there will be more time.
Anyway, thanks for 18 mails :) that scared me off this morning.

hpet
0
 
hpetAuthor Commented:
Oh, yeah I almost forgot this.
You can change height and width of dropdown list caraf_g.
Width is a bit easyer sending message CB_SETDROPEDWIDTH (or something like that).
Changing height of listbox is done through SetWindowPos function.

For example:
    SetWindowPos Combo1.hwnd, Combo1.hwnd, 0, 0, 150, 250, SWP_NOZORDER Or SWP_NOMOVE Or SWP_DRAWFRAME

Something like that (may be some typo)
Sizes are all in pixels!
0,0,150,250 stands for left-top and bottom-right where 150 is width of combo.
This is ok because combobox is not verticaly sizeable. If it would be, then you must find handle to listbox (like with GetWindow and find proper child - if more of them)) then just use that handle in setwindowpos..
but first solution works find VB combobox. You call it once and it is there until termination.

hpet

0
 
caraf_gCommented:
Forms 2.0 - yep I hate the bugger, but I thought I'll mention it anyway and let you make your own mind up ;-)
0
 
TimCotteeCommented:
hpet, I followed the same thoughts with SetWindowPos on the list box portion including getting the child window handle but it still didn't seem to change anyway. I shall put it on the backburner for now.
0
All Courses

From novice to tech pro — start learning today.