Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB5,VB6
Expertise: beginner
Oct 6, 2001



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Determine the optimal width for ListView columns

Here's a simple but effective trick to ensure that the column of a ListView control is wide enough to display the entire string you're assigning to the column title or to an element. Just drop a Label on the form, set its Autosize property to True and Visible property to False. Also, ensure that the Label control has a Font property that matches the one used for the ListView.

Then, when you want to ensure that a string will be displayed correctly in the ListView, just assign it to the Label control first, and then compare the Label's Width property with the width of the ListView column:

    Dim text As String

    text = "A very long string added to the ListView"
    ' Add a couple of spaces for good measure.
    Label1.Caption = text & "  "

    ' Compare current column width with Label's width     
    If ListView1.ColumnHeaders(1).Width < Label1.Width Then
        ' make the column wider if necessary
        ListView1.ColumnHeaders(1).Width = Label1.Width
    End If
    ListView1.ListItems.Add , , text
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date