HOW TO automatically adjust a listbox size (width and height) based on contents. NO SCROLLBAR WANTED

I want to be able to re-adjust the size of a ListBox whenever an item is added or removed from the list. Preferrable I'd like to do this through API calls. Any thoughts?

Thanks
const71Asked:
Who is Participating?
 
JR2003Connect With a Mentor Commented:
This will do both Height and Width. It doesn't use API calls but it does the job well.

Option Explicit

Private Sub Form_Load()
    With Label1
        .Font = List1.Font
        .AutoSize = True
        .Visible = False
    End With
End Sub

Private Sub Command1_Click()
    With List1
        .AddItem String(Rnd * 50, "x")
    End With
    ResizeList List1
End Sub

Private Sub Command2_Click()
    With List1
        If .ListCount > 0 Then
            .RemoveItem .ListCount - 1
        End If
    End With
    ResizeList List1
End Sub

Private Function ResizeList(MyListBox As ListBox)
    Dim i As Long
    Dim iMaxWidth As Long
    With MyListBox
        For i = 0 To .ListCount - 1
            Label1.Caption = .List(i)
            If iMaxWidth < Label1.Width Then
                iMaxWidth = Label1.Width
            End If
        Next i
        .Width = iMaxWidth
        .Height = .ListCount * Label1.Height + 60
    End With
End Function
0
 
JR2003Commented:
Add 2 buttons, a listbox and a label to a form and try the following code. It relys on the Autosize propert of a label.
JR

Option Explicit

Private Sub Form_Load()
    With Label1
        .Font = List1.Font
        .AutoSize = True
        .Visible = False
    End With
End Sub

Private Sub Command1_Click()
    With List1
        .AddItem String(Rnd * 50, "x")
    End With
    ResizeListWidth List1
End Sub

Private Sub Command2_Click()
    With List1
        If .ListCount > 0 Then
            .RemoveItem .ListCount - 1
        End If
    End With
    ResizeListWidth List1
End Sub

Private Function ResizeListWidth(MyListBox As ListBox)
    Dim i As Long
    Dim iMaxWidth As Long
    With MyListBox
        For i = 0 To .ListCount - 1
            Label1.Caption = .List(i)
            If iMaxWidth < Label1.Width Then
                iMaxWidth = Label1.Width
            End If
        Next i
    End With
    MyListBox.Width = iMaxWidth
End Function
0
 
const71Author Commented:
Thanks for suggestion but is there a way to do this without having to rely on other controls. Seems like sort of a hack to me. Pretty creative though.

Is there an API that can do this for both width and height?
 
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
EDDYKTConnect With a Mentor Commented:
close enough

Dim Tmp As String

With List1
    .Clear
    Tmp = String(Rnd * 50, "x")
       .AddItem Tmp
   End With
List1.Width = Me.TextWidth(Tmp) + Me.TextWidth("W")
0
 
const71Author Commented:
EDDYKT, whats the extra W for?
0
 
EDDYKTCommented:
Just make sure it leave some extra space at the end
0
 
fadlounConnect With a Mentor Commented:
EDDYKT Solution works only if you specify same font properties for the form and list control
0
All Courses

From novice to tech pro — start learning today.