Select Text Upon EntryMDI PDF File Viewer with Tabbed Header

Enlarge Combobox Dropdown Area

June 19th, 2007

An early version of the dropdown comboboxIn this second part of my series revisiting functions I wrote for VB6 I decided to see how one might implement a VB.NET routine to enlarge a combobox’s dropdown area to the size of the largest string that’s loaded into it. In VB6, as you can see in my original article, Enlarge Dropdown Area, it required a few API calls to pull it off. Has this gotten any easier to do in VB.NET?

The answer is, yes, it is much easier.

The combobox now has a property called DropDownWidth that you can use to set the width of the dropdown area. It couldn’t be much easier. Just put in the width you want in the properties box at design time and you’re set.

But, what if you don’t know how long the values will be at design time? You could estimate or you could use code to get the length of the strings you add and dynamically size the combo. Let’s look at one such routine.

Private Sub AddComboItemWithWidth(ByVal comboToLoad As ComboBox, ByVal itemText As String, ByVal maxStringWidth As Integer)
    Dim MyGraphics As System.Drawing.Graphics = Me.CreateGraphics
    Dim StringSize As Integer = CInt(MyGraphics.MeasureString(itemText, comboToLoad.Font, maxStringWidth).Width) + 1
    If comboToLoad.DropDownWidth < StringSize Then
        comboToLoad.DropDownWidth = StringSize
    End If
    comboToLoad.Items.Add(itemText)
End Sub

In this routine we pass in the combobox that we want to load, the value of the string, and the maximum length we want the combobox dropdown to be in pixels (it might be a good idea to base this user screen size or other such factors). First, we create a graphics object using the current form’s method. If you wanted to have this routine outside of the confines of a single form you could pass this object in as a parameter. Next, we use the MeasureString method to get the number of pixels required by the string. If the size is greater than the maximum size the max is returned. I add one to the returned number for a little extra padding but you don’t need to do this since the method will do some padding for you. Next, the returned size is compared against the current size and, if it is greater, the combo’s DropDownWidth property is increased. Lastly, the item is added to the combo.

If you wanted to resize the combo after loading, you could do the following:

Private Sub SizeCombo(ByVal comboToSize As ComboBox, ByVal maxStringWidth As Integer)
    Dim MyGraphics As System.Drawing.Graphics = Me.CreateGraphics
    Dim StringSize As Integer
    For Each ItemValue As String In comboToSize.Items
        StringSize = CInt(MyGraphics.MeasureString(ItemValue, comboToSize.Font, maxStringWidth).Width) + 1
        If comboToSize.DropDownWidth < StringSize Then
            comboToSize.DropDownWidth = StringSize
        End If
    Next
End Sub

This routine works in the same way except that it sizes after the combo is loaded.

So, it seems that doing this is a lot easier and simpler in .NET than it was in VB6.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB6 To VB.NET


Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (No Ratings Yet)
Loading ... Loading ...

1 Comment Add your own

  • 1. Dynamische ComboBox &laqu&hellip  |  April 9th, 2008 at 9:39 am

    [...] der Breite an den längsten Eintrag ist ebenfalls mit wenig Aufwand möglich, wie man z.B. hier nachlesen [...]

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles

Categories

Most Recent Articles

Feeds

 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities

Archives