ListView Final Column Autosize creates scrollbar

前端 未结 8 1259
闹比i
闹比i 2021-01-13 18:23

I am implementing a custom control which derives from ListView.

I would like for the final column to fill the remaining space (Quite a common task), I have gone abou

8条回答
  •  半阙折子戏
    2021-01-13 18:58

    Try using the ClientSize property. As MSDN puts it:

    The client area of a control is the bounds of the control, minus the nonclient elements such as scroll bars, borders, title bars, and menus.

    Edit: I've added a code sample demonstrating how achieve different fill behaviors.

    public class MyListView : ListView
    {
        public MyListView()
        {
            View = View.Details;
            Columns.Add(Column1);
            Columns.Add(Column2);
            Columns.Add(Column3);
        }
    
        private readonly ColumnHeader Column1 = new ColumnHeader();
        private readonly ColumnHeader Column2 = new ColumnHeader();
        private readonly ColumnHeader Column3 = new ColumnHeader();
    
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
    
            // Constant width
            Column1.Width = 200;
            // 50% of the ListView's width
            Column2.Width = (ClientSize.Width/2);
            // Fill the remaining width, but no less than 100 px
            Column3.Width = Math.Max(100, ClientSize.Width - (Column1.Width + Column2.Width));
        }
    }
    

提交回复
热议问题