自动调整包含tableLayoutPanel的面板

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

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;

行的高度大小正确。 我希望面板自动设置高度(我会在执行过程中隐藏一些行)。 其实我有:

在这里输入图像描述

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

在这里输入图像描述

我该如何做到这一点?

在这里输入图像描述

如果在执行过程中添加/删除行,则必须更新面板大小。


看看“Control.SetBoundsCore”是否可以帮助你。 您将不得不创建自己的面板才能使用它。

看来你的问题的原因是下面的代码行

this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Fill;
链接地址: http://www.djcxy.com/p/61573.html

上一篇: Autosize panel containing tableLayoutPanel

下一篇: controls with same name in different panels in C#