自动调整包含tableLayoutPanel的面板

时间:2017-03-08 16:43:26

标签: c# winforms

我有一个包含面板的元素:

this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.tableLayoutPanel1.SetColumnSpan(this.panel1, 2);
this.panel1.Controls.Add(this.tableLayoutPanel2);
this.panel1.Location = new System.Drawing.Point(10, 10);
this.panel1.Margin = new System.Windows.Forms.Padding(10);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(264, 100);
this.panel1.TabIndex = 2;

此面板包含tableLayoutPanel。 tableLayoutPanel有2行自动调整大小,包含标签/按钮:

this.tableLayoutPanel2.ColumnCount = 2;
this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel2.Controls.Add(this.button2, 1, 0);
this.tableLayoutPanel2.Controls.Add(this.button3, 1, 1);
this.tableLayoutPanel2.Controls.Add(this.label1, 0, 0);
this.tableLayoutPanel2.Controls.Add(this.label2, 0, 1);
this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel2.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel2.Name = "tableLayoutPanel2";
this.tableLayoutPanel2.RowCount = 2;
this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel2.Size = new System.Drawing.Size(262, 98);
this.tableLayoutPanel2.TabIndex = 0;

行的高度尺寸正确。我希望面板自动设置他的高度(我会在执行期间隐藏一些行)。其实我有:

enter image description here

当我为panel1设置autoSize = true时,我有:

enter image description here

我该怎么办呢?

enter image description here

如果我在执行期间添加/删除行,则必须更新面板大小。

1 个答案:

答案 0 :(得分:1)

查看“Control.SetBoundsCore”是否可以为您提供帮助。您必须创建自己的面板才能使用它。

似乎您的问题的原因是遵循代码行

this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Fill;