I have a custom list view control in WinForms that applies some custom styles. This works well, however I have a part of the control that is not covered when the user maximises the screen (columns are set to fixed width) and ColumnHeaderAutoResizeStyle.None is set. I'd like to have the last column auto fill the gap.
To achieve this inside of the custom control I have added the following code to ListView.SizeChanged event.
Private Sub CustomListView_Resized(sender As Object, e As EventArgs) Handles Me.SizeChanged
Try
If (Not _isResizing) Then
_isResizing = True
Dim myListView As customListView = DirectCast(sender, customListView)
' Work out current column widths
Dim totalColumWidthInPx As Integer = 0
For i = 1 To Columns.Count - 1
totalColumWidthInPx += Me.Columns(i).Width
Next
' Increment the final column by the difference in width to make up the gap
Me.Columns.Item(Columns.Count - 1).Width += (myListView.ClientRectangle.Width - totalColumWidthInPx)
End If
Finally
_isResizing = False
End Try
End Sub
This is always giving me inconsistent results, the column width is incremented to much and adds the scrollbars as below.
Little bit of debug info.
Control: lvAppointments, Width (px): 701, Client Rectangle Width (px): 697, All Column Widths: 648, Diff: 49, Last Column: 234, Last Column + Diff: 283