Listbox width by widest Listbox item
I am trying to create a ListboxRemoveItem sub.
What I want to accomplish:
' Always set Listbox.width equal to the widest Listbox item.tostring
Sub ListboxRemoveItem(byVal item as object)
' Remove item from Listbox
' After removal of variable 'item' in Listbox, find new widest item.
' Set Listbox width = to this item's width.
End Sub
Any suggestions?
[520 byte] By [
TMB] at [2007-12-24]
The following routine will work:
Dim gfx As System.Drawing.Graphics = Me.ListBox1.CreateGraphics
For Each o As Object In Me.ListBox1.Items
Dim sz As SizeF
sz = gfx.MeasureString(o.ToString, Me.ListBox1.Font)
If Me.ListBox1.Width < sz.Width Then
Me.ListBox1.Width = sz.Width
End If
Next
gfx.Dispose()
Don't know how many list items you'll have... but this should be good for quite a few.
HTH