Drop Down Control Autofit Width

Posted on 2004-11-09
Last Modified: 2008-03-17
I'm looking for a simple drop down control that will autofit to the widest of the drop down items. If anyone has any suggestions I would appreciate them. This is to be used in .net webform.
Question by:jamesh1031
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Perhaps somethign like this will work for you:

        Private Sub SizeComboBox(ByVal cb As ComboBox)
            Dim s As String
            Dim sSize As SizeF
            Dim g As Graphics = cb.CreateGraphics
            For Each s In cb.Items
                sSize = g.MeasureString(s, cb.Font)
                If sSize.Width > cb.Width Then
                    ' not sure how to properly account for the width
                    ' of the vertical scrollbar which is why the 30 is added
                    cb.Width = sSize.Width + 30
                End If
        End Sub
    LVL 48

    Expert Comment

    LVL 48

    Expert Comment

    LVL 28

    Expert Comment

    'make combo width = the longest item in combo
    'this will NOT change the width of combo but will change the width of dropdown part

     Dim g As Graphics
            g = cmb.CreateGraphics()
            Dim o As Object
            Dim x As Integer = 0
            Dim maxWidth As Integer = 135' limit width to this
            For Each o In cmb.Items
    'for reading from dtaset
                'Dim w As Integer = CInt(g.MeasureString(DataSet.Tables(0).Rows(x).Item(1).ToString, cmb.Font).Width)
    'based on the items in combo
                Dim w As Integer = CInt(g.MeasureString(cmb.Items(x), cmb.Font).Width)
                If (w > maxWidth) Then
                    maxWidth = w
                End If
                x = x + 1
           cmb.DropDownWidth = maxWidth

    Author Comment

    I did mention this is a web form?
    LVL 28

    Accepted Solution

    Hi jamesh1031,

    but the normal dropdownlist is resized in width to the the length of the wides(the item with max count of letters) item automatically!

    LVL 3

    Expert Comment

    mmarinov is correct. Unless you set specific style attributes, the dropdoen control takes the size of the widest element.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Flash ( has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now