Solved

Help setting combobox dropdown list width

Posted on 2016-08-08
4
72 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 34

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 34

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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

707 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