VB.NET – Liste déroulante / combobox largeur automatique en fonction items / contenu

Private Sub cbSaisine_DropDown(sender As System.Object, e As System.EventArgs) Handles cbSaisine.DropDown

Dim senderComboBox As ComboBox = DirectCast(sender, ComboBox)

Dim width As Integer = senderComboBox.DropDownWidth

Dim g As Graphics = senderComboBox.CreateGraphics()

Dim font As Font = senderComboBox.Font

Dim vertScrollBarWidth As Integer = If((senderComboBox.Items.Count > senderComboBox.MaxDropDownItems), SystemInformation.VerticalScrollBarWidth, 0)

Dim newWidth As Integer

For Each s As DataRowView In DirectCast(sender, ComboBox).Items

newWidth = CInt(g.MeasureString(s(1).ToString, font).Width) + vertScrollBarWidth

If width < newWidth Then

width = newWidth

End If


senderComboBox.DropDownWidth = width

End Sub


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s