views:

153

answers:

1

How can I have a single column resize with the form so that the ListView columns continue to fill the whole form?

+1  A: 

Yes, implement the listview's Resize event handler and calculate the space left for the column. For example:

Private Sub ListView1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.Resize
    Dim resizeColumn As Integer = 1
    Dim w As Integer = 0
    For column As Integer = 0 To ListView1.Columns.Count - 1
        if column <> resizeColumn then w += ListView1.Columns(column).Width
    Next
    w = ListView1.ClientSize.Width - w - 1 - SystemInformation.VerticalScrollBarWidth
    If w > 0 Then ListView1.Columns(resizeColumn).Width = w
End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    ListView1_Resize(Me, EventArgs.Empty)
    MyBase.OnLoad(e)
End Sub
Hans Passant
That worked well, however how can I select which column to resize?
Ben
Of course, just change the code.
Hans Passant
Herpderp.Which part? I tried changing almost all the variables and got no good results.
Ben
@Ben: code updated. Change "resizeColumn" to the column you want to get resized.
Hans Passant
Thanks a lot Hans, appreciate it!
Ben
@Ben, this wasn't helpful? Sounds like it was, a "this was helpful" upvote is appropriate perhaps.
Hans Passant