0

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

customlistview

Joey Bob
  • 312
  • 8
  • 21
  • `AutoResizeColumns` is not a property you set and it keeps updating the sizes as you add items - it is a method you call to resize/fit the current columns for the size. See [ListView Column AutoSizing](http://stackoverflow.com/a/24106546/1070452). To resize or fit to avold the HSCroll see [Fit Columns to Listview for Vertical Scrollbar](http://stackoverflow.com/a/24722635/1070452) – Ňɏssa Pøngjǣrdenlarp Jun 17 '15 at 12:24
  • Thanks for the link I will try this and see how I get on. – Joey Bob Jun 18 '15 at 11:17

0 Answers0