![](/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.