Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15587

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

$
0
0
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


Viewing all articles
Browse latest Browse all 15587

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>