Drop Down Control Autofit Width

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

'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
jamesh1031Author Commented:
I did mention this is a web form?
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!


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mmarinov is correct. Unless you set specific style attributes, the dropdoen control takes the size of the widest element.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.