Autosize panel containing tableLayoutPanel

I have an element containing a panel :

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;

This panel contains a tableLayoutPanel. The tableLayoutPanel has 2 rows auto sized containing labels/buttons :

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;

The heights for the rows are correctly sized. I want that the panel set his height automatically (I will hide some row during execution). Actually I have :

在这里输入图像描述

When I set autoSize = true for panel1 I have :

在这里输入图像描述

How can I do to have this ?

在这里输入图像描述

Panel size must be updated if I add/remove row during execution.


See if "Control.SetBoundsCore" can help you. You will have to create your own panel to use it then.

It seems the reason of your problem is following line of code

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

上一篇: 如何在可滚动面板中隐藏动态创建的控件

下一篇: 自动调整包含tableLayoutPanel的面板