Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement a nice one autoresize mechanism for Listview columns?

I would like to add a decent AutoResize mechanism to my Listview but I don't know what to do.

I set the last column to Autoresize itselfs when the Form resizes:

Private Sub Form1_Resize(sender As Object, e As EventArgs) _
Handles MyBase.Resize

    ColumnDownload.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize)

End Sub

But this native autoresize method is so wrong because when I resize the application, the horizontal scrollbar is shown:

enter image description here

enter image description here

Ofcourse If I pick by myself with the mouse the last column separator to increase the column width then I want to see/use the horizontal scrollbar, but the framework autoresize just autoincreases the size displaying that horizontal scrollbar, is so ugly an uneficient there's no need to auto display that scrollbar when resizing the form!

The look that I would is just like JDownloader and all the nice programs has, an Autoresize that stops exactly on the start of the vertical scrollbar without showing the ugly horizontal scrollbar if not needed, for example like this:

enter image description here

I hope that you could understand my question.

EDIT:

'Magic solutions' such as these:

MyColumnHeader.Width = -2
MyColumnHeader.Width = -1

...Does the same. See good article here: http://visualbasic.about.com/od/quicktips/qt/ListViewQT.htm

Really all the Google search results about resizing a Listview in .NET are using that, any proportional resize or alternative, any decent auto resizer, incredible.

like image 939
ElektroStudios Avatar asked Dec 19 '13 15:12

ElektroStudios


1 Answers

ObjectListView, a open source wrapper around a .NET ListView, has a FillsFreeSpace property which does exactly this.

As the user resizes the control, the column will become larger or smaller, such that the horizontal scroll bar is never shown. It was relatively complicated to implement correctly :)

See this link for more details.

[Edit]

Basic scheme:

  1. Listen for Resize event on the ListView.
  2. Calculate the width of all columns except the last
  3. Resize the last column to the difference of the width of the other columns and the ClientSize of the ListView.

This will work:

private void ResizeLastColumn(int listViewWidth)
{
    int totalColumnWidth = 0;
    for (int i =0 ; i < listView1.Columns.Count - 1; i++) {
        totalColumnWidth += listView1.Columns[i].Width;
    }

    colLast.Width = Math.Max(listViewWidth - totalColumnWidth, 10);
}

private void listView1_Resize(object sender, EventArgs e)
{
    ResizeLastColumn(this.listView1.ClientSize.Width);
}

The limitation on this is that the horizontal scroll bar will flicker slightly when shrinking the control. To get around that, you need to intercept the WM_WINDOWPOSCHANGING message, and resize the columns to the size that the control is going to become. This is somewhat complicated -- and exactly what ObjectListView does :)

like image 182
Grammarian Avatar answered Sep 19 '22 21:09

Grammarian