![](/img/trans.png)
[英]How do I apply column and row styles on a TableLayoutPanel programmatically?
[英]Column Styles not working on TableLayoutPanel
我有一个TableLayoutPanel,它具有由用户确定的动态数量的列和行。 我希望里面的按钮是正方形且大小相同,但是每当我使用循环设置列/行样式时,它们就永远不会成为我想要的大小。
如何获得列/行样式以设置容器元素的适当宽度和高度?
这是处理设置表的宽度大小的代码的循环方法(我对行使用类似的方法)
void FormatTableWidth(ref TableLayoutPanel container)
{
TableLayoutColumnStyleCollection columnStyles = container.ColumnStyles;
foreach (ColumnStyle style in columnStyles)
{
style.SizeType = SizeType.Absolute;
style.Width = 60;
}
}
你可以这样...
public void AddButtontControls()
{
tblPanel.SuspendLayout();
tblPanel.Controls.Clear();
tblPanel.GrowStyle = TableLayoutPanelGrowStyle.FixedSize;//.AddColumns;
tblPanel.ColumnStyles.Clear();
for (int i = 0; i < tblPanel.ColumnCount; i++)
{
ColumnStyle cs = new ColumnStyle(SizeType.Percent, 100 / tblPanel.ColumnCount);
tblPanel.ColumnStyles.Add(cs);
//Add Button
Button a = new Button();
a.Text = "Button " + i + 1;
tblPanel.Controls.Add(a);
}
tblPanel.ResumeLayout();
}
抱歉告诉您,但您使用的控件不正确。 您绝对必须使用FlowLayoutPanel控件,并且可以在其中添加任意数量的控件,可以告诉哪个方向将填充该控件(是否包装内容)以及许多其他控件。
最重要的是-它不会像TableLayoutPanel那样闪烁:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.