autoresize columns in a checklistbox

Aquitus 146 Reputation points
2022-02-17T14:08:46.397+00:00

the items inserted into my checklistbox can vary in size but the column width is fixed and im not sure how to collect the largest item's width from the collection
how should i go about coding an autoresize function?

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,141 Reputation points
    2022-02-17T14:30:06.73+00:00

    Hi
    One way is to measure each of the strings in the ListBox, keeping the largest as you iterate through and after iteration finished, set the ListBox width accordingly.

    You can measure a string, with the appropriate font: below is an example. You can fine tune the width returned by adding a character or two to the string being measured to suit your needs.

    ' Test code
     Dim r As New Random
     Dim s As String = "abcdefghijklmnopqrstuvwxyz"
     s &= s.ToUpper & "1234567890"
     With ListBox1
     For i As Integer = 0 To 19
     Dim c As Integer = r.Next(5, 15)
     .Items.Add(s.Substring(r.Next(0, s.Length - c), c))
     Next
     End With
     ListBox1.Width = GetWidth()
    
     Function GetWidth() As Integer
     Dim w As Integer = 0
     For Each s As String In ListBox1.Items
    '  the "XXX" is just extra spacing
     Dim ww As Integer = TextRenderer.MeasureText(s & "XXX", ListBox1.Font).Width
     If ww > w Then w = ww
     Next
     Return w
     End Function
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2022-02-18T06:02:13.753+00:00

    Hi @Aquitus ,
    As suggested by LesHay-2099, you can refer to the following code to resize the checklistbox.

        Private Sub ResizeSize(ByVal lst As ListBox)  
            Dim maxWidth As Integer = 0  
            Dim h As Integer = lst.ItemHeight * lst.Items.Count  
            lst.Height = h + lst.Height - lst.ClientSize.Height  
      
            For Each s As String In lst.Items  
                Dim itemWidth As Integer = TextRenderer.MeasureText(s & "XXX", lst.Font).Width  
                If itemWidth > maxWidth Then maxWidth = itemWidth  
            Next  
            lst.Width = maxWidth  
        End Sub  
    

    Hope it could be helpful.
    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-02-17T19:44:47.387+00:00

    Consider using a ListView which has AutoResizeColumn to resize a text based column and add a column for check boxes. To get checked items use CheckedItems.

    You can set selection mode to 1 and FullRowSelect to true.

    When done the above and a few other mods in the property windows of the ListView you will be good to go.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.