Hi, I have a WinForm application that has a list view in it. What I want to be able to do is have the right most column resize (width) whenever the window gets bigger or smaller. Is this possible? If so, which property is it controlled by?
A:
The ListView
has a Columns
property, which is a collection containing columns, and each of those columns has a Width
property:
var widthOfLastColumn = listView.Columns[ listView.Columns.Count - 1 ].Width;
listView.Columns[ listView.Columns.Count - 1 ].Width = newWidth;
In order to keep the width of the last column such that it fills the rest of the list view, you could add something like the following to your Form’s Resize
event:
var width = 0;
for (int i = 0; i < listView.Columns.Count - 1; i++)
width += listView.Columns[i].Width;
listView.Columns[ listView.Columns.Count - 1 ].Width =
listView.ClientSize.Width - width;
You may have to subtract a bit more to avoid the horizontal scrollbar in case there is padding between the columns — I didn’t test it.
Timwi
2010-08-31 22:16:41