Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

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.
0
jamesh1031
Asked:
jamesh1031
1 Solution
 
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
        Next
        g.Dispose()
    End Sub
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
iboutchkineCommented:
'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
        Next
        g.Dispose()
       cmb.DropDownWidth = maxWidth
0
 
jamesh1031Author Commented:
I did mention this is a web form?
0
 
mmarinovCommented:
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!

Regards!
B..M
mmarinov
0
 
skpatraCommented:
mmarinov is correct. Unless you set specific style attributes, the dropdoen control takes the size of the widest element.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now