I have a listView with multiple columns. I want to save columns width everytime user changes them, to be able to restore values on next start of the application to have columns width as user adjusted before. Same as what happens in apps like download managers. How can I code it?
My other question is about how to get a column name at a specific index.
Ok, I found the solution and as no one answered my question, I want to share how I did this.
Add "ColumnWidthChanged" event handler:
this.listView1.ColumnWidthChanged += new System.Windows.Forms.ColumnWidthChangedEventHandler(this.listView1_ColumnWidthChanged);
Add a "ColumnWidthChanged" event to check columns width after changing any of them. Then make a string and store column widths with their names in it.
private void listView1_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
{
StringBuilder widths = new StringBuilder();
widths.Clear();
for (int i = 0; i < listView1.Columns.Count; i++)
{
int columnWide = listView1.Columns[i].Width;
string columnName = listView1.Columns[i].Text;
widths.Append(columnName + ":" + columnWide.ToString() + "#");
}
string line= widths.ToString();
}
The result would be like this: column1:xx#column2:xx#column3:xx ...
Now you can save this string to a file to be able to retrieve and restore sizes on next app start. You can easily extract and use the values using line.Split('#'), to set column sizes on next app start.