How to resize combobox?

  • Thread starter Thread starter dfetrow410
  • Start date Start date
D

dfetrow410

How to I resize the combobox and set it a bit passed the largest string
in the box. By the way, the box gets built by a db table. Thanks for
your help


Dave
 
Dave,

You can change the width of the combobox via its Size property, but you
cannot change the height of the combobox. The width depend on the font used
to print the items. The bigger the font, the taller the combobox.
If you want to have control over the combobox height you need to implement
owner-draw combobox. In this case you can set the ItemHeight property.

If you want change the width of the combobox depending on the lenght of the
text you can measure the text using a graphics object created from the
combobox control. You need to take into cosideration the pull-down button. I
believe it is related to the size of the scroll bart thus you can use
SystemInformation.VerticalScrollBarWidth.
 
Try this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

With ComboBox1.Items
.Clear()
.Add("AAA")
.Add("BBBBBB")
.Add("CCCCCCCCC")
.Add("DDDDDDDDDDDD")
.Add("EEEEEEEEEEEEEEE")
.Add("WWWWWWWWWWWWWWWWWWWWWW1")
End With

With ComboBox1
Dim g As Graphics = ComboBox1.CreateGraphics
Try
For index As Integer = 0 To .Items.Count - 1
Dim size As SizeF = g.MeasureString(.Items(index), .Font)
size.Width += SystemInformation.VerticalScrollBarWidth
If .Width < size.Width Then .Width = size.Width
Next
Finally
g.Dispose()
End Try
End With

End Sub

I hope this helps,

Joris
 
Back
Top