gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:19 26 4
gpt4 key购买 nike

一开始有一个只有一行没有列的 TableLayoutPanel。通过按下按钮,我希望此面板转换为5 行和 3 列

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

问题是你得到这个结果!

enter image description here

如您所见,使用行和列创建的框在面板中不包含相同的区域。

在 3 列和 5 行的情况下,列必须共享 tablelayoutpanel 的 100%,因此每列 30%。 5行的情况下,每行必须占20%。

所以我将接下来的两行代码附加到 button_Click 方法。

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

但我得到了相同的结果!我缺少什么?

最佳答案

您可能正在向已定义的 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 });

关于c# - 如何以编程方式在 TableLayoutPanel 上应用列和行样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38724425/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com