2017-03-08 28 views
0

自動調整面板I具有包含面板的元素:含有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; 

行的高度大小正確。我希望面板自動設置高度(我會在執行過程中隱藏一些行)。其實我有:

enter image description here

當我設置的autoSize =真爲PANEL1我:

enter image description here

我該怎麼做纔能有這樣的?如果我添加/執行過程中刪除行

enter image description here

面板尺寸必須更新。

回答

1

看看「Control.SetBoundsCore」可以幫助你。您將不得不創建自己的面板才能使用它。

看來你的問題的原因是下面的代碼

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

它的工作原理線謝謝。我只需要爲面板和佈局設置autosize = true。當我刪除一行時,我強制高度爲0,並且自動調整大小非常合適。 –