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




Reply With Quote
