Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help setting combobox dropdown list width

Hi,


How do you adjust the width of the dropdown list of a combobox based on the size of the text?

Thanks,

Victor
Avatar of it_saige
it_saige
Flag of United States of America image

At it's simplest -

Form1.vb -
Public Class Form1
	ReadOnly items As New List(Of String)({"TEXT", "LONGTEXT", "LONGERTEXT", "LONGESTTEXT", "LONGTEXTTHATISLONGERTHANTHELONGEST"})
	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
		ComboBox1.DataSource = items
		ComboBox2.DataSource = items
		ComboBox3.DataSource = items
		AutoSizeComboBox(ComboBox3)
	End Sub

	Private Sub AutoSizeComboBox(cb As ComboBox)
		Using g As Graphics = cb.CreateGraphics()
			Dim maxWidth = 0.0F
			For Each item In cb.Items
				maxWidth = Math.Max(maxWidth, g.MeasureString(item.ToString(), cb.Font).Width)
			Next
			cb.Width = CType(maxWidth + 20, Integer)
		End Using
	End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.ComboBox1 = New System.Windows.Forms.ComboBox()
		Me.ComboBox2 = New System.Windows.Forms.ComboBox()
		Me.ComboBox3 = New System.Windows.Forms.ComboBox()
		Me.SuspendLayout()
		'
		'ComboBox1
		'
		Me.ComboBox1.FormattingEnabled = True
		Me.ComboBox1.Location = New System.Drawing.Point(13, 13)
		Me.ComboBox1.Name = "ComboBox1"
		Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
		Me.ComboBox1.TabIndex = 0
		'
		'ComboBox2
		'
		Me.ComboBox2.FormattingEnabled = True
		Me.ComboBox2.Location = New System.Drawing.Point(13, 41)
		Me.ComboBox2.Name = "ComboBox2"
		Me.ComboBox2.Size = New System.Drawing.Size(329, 21)
		Me.ComboBox2.TabIndex = 1
		'
		'ComboBox3
		'
		Me.ComboBox3.FormattingEnabled = True
		Me.ComboBox3.Location = New System.Drawing.Point(13, 69)
		Me.ComboBox3.Name = "ComboBox3"
		Me.ComboBox3.Size = New System.Drawing.Size(121, 21)
		Me.ComboBox3.TabIndex = 2
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(354, 261)
		Me.Controls.Add(Me.ComboBox3)
		Me.Controls.Add(Me.ComboBox2)
		Me.Controls.Add(Me.ComboBox1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
	Friend WithEvents ComboBox2 As System.Windows.Forms.ComboBox
	Friend WithEvents ComboBox3 As System.Windows.Forms.ComboBox

End Class

Open in new window

Produces the following output -User generated imageThe first and second combobox's are hard set to initial size and full form size whereas the third combo box uses the autosize method to resize according to it's items.
User generated image-saige-
Avatar of Victor  Charles

ASKER

Hi,

Thank you for the code, is there a  way to also set a naximum width when you use autosize?

Victor
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You.