簡體   English   中英

如何以編程方式在TableLayoutPanel上應用列和行樣式?

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

開頭有一個TableLayoutPanel,它只有一行,沒有列。 通過按一個按鈕,我希望此面板轉換為5行3列

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

問題是您得到了這個結果!

在此處輸入圖片說明

如您所見,使用行和列創建的框在面板中不包含相同區域。

在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 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM