简体   繁体   English

如何以编程方式在TableLayoutPanel上应用列和行样式?

[英]How do I apply column and row styles on a TableLayoutPanel programmatically?

In the beginning there is a TableLayoutPanel with only one row and no columns. 开头有一个TableLayoutPanel,它只有一行,没有列。 By the press of a button I want this panel to be transformed with 5 rows and 3 columns . 通过按一个按钮,我希望此面板转换为5行3列

private void button_Click(object sender, EventArgs e)
    {
        this.tableLayoutPanel1.ColumnCount = 3;
        this.tableLayoutPanel1.RowCount = 5;
    }

The problem is that you get this as a result! 问题是您得到了这个结果!

在此处输入图片说明

The boxes that are created with the rows and columns do not contain the same area in the panel, as you can see. 如您所见,使用行和列创建的框在面板中不包含相同区域。

In the case of 3 columns and 5 rows, the columns must share the 100% of the tablelayoutpanel, so 30% per each. 在3列和5行的情况下,这些列必须共享tablelayoutpanel的100%,因此每个共享30%。 In the case of 5 rows, each row must take 20%. 对于5行,每行必须占20%。

So I append to the button_Click method the next two lines of code. 因此,我将以下两行代码添加到button_Click方法。

this.tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 20F));

But I get the same result! 但是我得到了相同的结果! What I am missing? 我缺少什么?

Probably you are adding styles to an already defined TableLayoutPanel without resetting the current styles. 可能是将样式添加到已定义的TableLayoutPanel中,而不重置当前样式。

tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tableLayoutPanel1.Dock = DockStyle.Fill;

tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.ColumnStyle.Clear();
tableLayoutPanel1.RowStyle.Clear();

tableLayoutPanel1.RowCount = 5;
for(int x = 0; x < tableLayoutPanel1.RowCount; x++)
    tableLayoutPanel1.RowStyles.Add(new RowStyle() { Height = 20, SizeType = SizeType.Percent });
tableLayoutPanel1.ColumnCount = 3;
for(int x = 0; x < tableLayoutPanel1.ColumnCount; x++)
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle() { Width = 33, SizeType = SizeType.Percent });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM