Solved

Help setting combobox dropdown list width

Posted on 2016-08-08
4
62 Views
1 Endorsement
Last Modified: 2016-08-13
Hi,


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

Thanks,

Victor
1
Comment
Question by:vcharles
  • 2
  • 2
4 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 41747884
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 -Capture.JPGThe 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.
Capture.JPG-saige-
1
 

Author Comment

by:vcharles
ID: 41750111
Hi,

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

Victor
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 41750977
Sure, you could do something like:
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, 20)
	End Sub

	Private Sub AutoSizeComboBox(cb As ComboBox, Optional minWidth As Integer = 50)
		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(Math.Min(maxWidth, minWidth) + 20, Integer)
		End Using
	End Sub
End Class

Open in new window

Which now produces the following output (I explicitly chose an extremely small width):Capture.JPG-saige-
0
 

Author Closing Comment

by:vcharles
ID: 41754829
Thank You.
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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