Automatically resize TableLayoutPanel

耗尽温柔 提交于 2019-12-23 16:38:56

问题


I have a programmatically created TableLayoutPanel. It works fine but I couldn't find something: how can I make it size columns automatically when the form is resized? The panel is set to Dock.Top and when I resize the form instead of sizing every column as percents, only last column grows. What can I do for this? Here's how I add ColumnStyle for each column:

this.tablePanelFilter.ColumnStyles.Add(
  new ColumnStyle(SizeType.Percent,Convert.ToSingle(
     Math.Ceiling((decimal)100 / (decimal)columnCount))));

回答1:


Have you tried setting the ColumnStyles to SizeType.Percent?

http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.columnstyles.aspx




回答2:


Do you clear the ColumnStyles first? Use a debugger and/or some code to verify that you have as many ColumnStyles as you have columns.




回答3:


Adding this code to form.Resize event solved the problem:

this.tablePanelFilter.ColumnStyles.Clear();

            for (int i = 0; i < this.tablePanelFilter.ColumnCount; i++)
            {
                ColumnStyle c = new ColumnStyle();
                c.SizeType = SizeType.Percent;
                c.Width = Convert.ToSingle(Math.Ceiling((decimal)100 / (decimal)this.tablePanelFilter.ColumnCount));
                this.tablePanelFilter.ColumnStyles.Add(c);
            }


来源:https://stackoverflow.com/questions/1487120/automatically-resize-tablelayoutpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!