Results 1 to 7 of 7

Thread: Auto resize ListView columns to fit either the header text or content

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2016
    Location
    UK
    Posts
    46

    Auto resize ListView columns to fit either the header text or content

    If the ListView has no items this will resize the columns to fit the header text but if it has items it will resize the columns to fit the content if the content is wider than the header text.

    Code:
        ''' <summary>
        ''' Automatically resizes a ListViews columns to fit either the column content or the column header text depending on which is the widest
        ''' </summary>
        ''' <param name="List_View"></param>
        Public Sub AutoColumnResize(List_View As ListView)
    
            Dim ColHeader As ColumnHeader
            Dim HeaderTextLength As Integer
            Dim MaxContentLength As Integer
            
            For i = 0 To List_View.Columns.Count - 1
    
                ColHeader = List_View.Columns(i)
                HeaderTextLength = List_View.Columns(i).Text.Length
    
                For inc = 0 To List_View.Items.Count - 1
                    MaxContentLength = Math.Max(MaxContentLength, List_View.Items(inc).SubItems(i).Text.Length)
                Next
    
                If MaxContentLength < HeaderTextLength Then
                    ColHeader.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize)
                End If
            Next
        End Sub
    Last edited by LucasCain; Jan 26th, 2021 at 08:50 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width