Private Sub Form_Load()
Dim a(1 To 2, 1 To 3) As String
a(1, 1) = "AAAAAAAAAA"
a(1, 2) = "BBBB999"
a(1, 3) = "CCCCCCC2312313131321321"
a(2, 1) = "AA897979797kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"
a(2, 2) = "BBBBBBBBBBBBBBB"
a(2, 3) = "CCC"
Me.ListBox1.List = a
Me.ListBox1.ColumnCount = 3
Call AutofitColumnListbox(Me, ListBox1)
End Sub
Public Sub AutofitColumnListbox(ByVal FormName As Object, ListBox As Object)
Dim Zeile As Long, Spalte As Long, tieude
Dim SpaltenBreiten As String, MaxBreite As Double
Dim Lbl As Object
Set Lbl = Me.Controls.Add("VB.Label", "Label1")
Lbl.WordWrap = False
Lbl.AutoSize = True
For Zeile = 0 To ListBox.ColumnCount - 1
MaxBreite = 0
For Spalte = 0 To ListBox.ListCount - 1
Lbl.Caption = ListBox.Column(Zeile, Spalte) & ",,,"
If MaxBreite < Lbl.Width Then
MaxBreite = Lbl.Width
End If
Next Spalte
SpaltenBreiten = SpaltenBreiten & CLng(MaxBreite + 1) & ";"
Next Zeile
ListBox.ColumnWidths = SpaltenBreiten
Set Lbl = Nothing
End Sub