开发者

ListView Column Size

开发者 https://www.devze.com 2023-04-05 07:40 出处:网络
I have a Win Form with two columns and I wanted to have the 1st column width 2/3 of the size of the ListView size and the second column 1/3. I wanted to be able to fill the ListView with the 2 columns

I have a Win Form with two columns and I wanted to have the 1st column width 2/3 of the size of the ListView size and the second column 1/3. I wanted to be able to fill the ListView with the 2 columns and be able to shrink and expand it to allow room for a vertical scroll bar if the ListView items goes below the bottom of the box. I have come up with this code but this only will take effect when the scroll bar is there, and even then it expands t开发者_如何学Che columns slightly so a horizonal scrollbar is created which I don't want. Any help is very appreciated.

private void AddColumn()
{
    listView1.View = View.Details;
    listView1.Columns.Add("Item");
    listView1.Columns.Add("Date Added");
    int itemColumnWidth = listView1.Width - SystemInformation.VerticalScrollBarWidth;
    listView1.Columns[0].Width = itemColumnWidth / 3 * 2;
    listView1.Columns[1].Width = itemColumnWidth / 3;
}


What you can do is resize the first column to be 2/3 width and set the second column to expand to the remaining width.

The method for expanding the second column to remaining width is explained in the answer to this question: ListView Final Column Autosize creates scrollbar


I've always had this problem. You must always subtract just a little more space for the scrollbar. At least subtract 4 additional pixels or to stay safe subtract 8px to 10px. No one will notice the little extra space.

I guess somebody didn't get their arithmetic right when they implemented listviews... There must be some internal padding of columns going on. The design seems to be focused on creating additional space when needed rather than on fitting the columns to the screen. Think of Windows Explorer: columns never resize to the size of the window, and there are always nasty scrollbars all around.

We all want our listview to look like Visual Studio's cleanly designed Error List but that control seems to be a different implementation that is always guaranteed to fit columns to the control's width.

0

精彩评论

暂无评论...
验证码 换一张
取 消