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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set Combobox Height

Status
Not open for further replies.

mjk9564

Programmer
Apr 23, 2003
64
US
Is there a way to change the height of a combobox w/o adjusting the font? I'm looking to create a custom combobox and would like to adjust its height.

Thanks!
Matt
 
It appears the only way that .NET will allow you to modify the height of a combo box is if it's Dropdownstyle is set to Simple. Of course, setting it to Simple makes it look like a textbox, and you can't drop it down to see all of the values.
 
Found this on another website. It works but I don't like the idea of a API call for each combobox. I will live with the default size.

Code:
Private Declare Auto Function SendMessage Lib "user32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32 _
) As Int32

Private Const CB_SETITEMHEIGHT As Int32 = &H153

Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load
SetComboEditHeight(Me.ComboBox1, 50)
End Sub

Private Sub SetComboEditHeight( _
ByVal Control As ComboBox, _
ByVal NewHeight As Int32 _
)
SendMessage(Control.Handle, CB_SETITEMHEIGHT, -1, NewHeight)
Control.Refresh()
End Sub

I thought this may help someone later on because it wasn't easy to find.
 
If I'm not mistaken that property only makes the combobox not grow if you change the font size. What I'm looking for is a way to set the height.
 
There is a way to do this, but I don't know if the results will be what you want.

First, set the combobox's DrawMode property to OwnerDrawFixed.

Then set the ItemHeight property to a value that makes the combobox be the height you want.

Finally, put this in the combobox's DrawItem event handler:

Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem

If e.Index < 0 Then Return

e.Graphics.DrawString(ComboBox1.Items(e.Index), Font, New SolidBrush(Color.Black), e.Bounds.X + 1, e.Bounds.Y + 1)

End Sub

You can play around with it a bit - maybe with the DrawFocusRectangle and the DrawBagkground methods - to get it to look a little better. They would be in the DrawItem event:

e.DrawBagkground
e.DrawFocusRectangle

Hope this helps!

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top