2013-07-18 544 views
5

我有一個簡單的1x3 TableLayoutPanel。我想實現一個非常簡單的事情:當窗口被調整大小時,調整中間行的大小並保持頂部和底部相同。我試圖做出合乎邏輯的事情,即設置剛性的頂部和底部的行尺寸併爲中間行自動調整大小。不幸的是,這是調整底部行。調整窗口大小時自動調整TableLayoutPanel行的大小

// 
// tableLayoutPanel1 
// 
this.tableLayoutPanel1.ColumnCount = 1; 
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); 
this.tableLayoutPanel1.Controls.Add(this.topPanel, 0, 0); 
this.tableLayoutPanel1.Controls.Add(this.middlePanel, 0, 1); 
this.tableLayoutPanel1.Controls.Add(this.bottomPanel, 0, 2); 
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; 
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); 
this.tableLayoutPanel1.Name = "tableLayoutPanel1"; 
this.tableLayoutPanel1.RowCount = 1; 
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 140F)); 
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); 
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 24F)); 
this.tableLayoutPanel1.Size = new System.Drawing.Size(1102, 492); 
this.tableLayoutPanel1.TabIndex = 19; 

所有的內部面板都將Dock設置爲Fill和默認錨點。我究竟做錯了什麼?

回答

8

將中間行更改爲100%,這將告訴系統中間行將填補剩下的任何空缺。因此,改變這種(我相信這是你的designer.cs):

this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); 

到:

this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); 

檢查TableLayoutPanel.RowStyle from MSDN

  1. 行與RowStyle設置爲絕對的第一考慮,並且他們的固定高度被分配。
  2. RowStyle設置爲AutoSize的行的大小根據其內容而定。
  3. 剩餘空間在RowStyle設置爲Percent的行之間分配。
1

只需設置絕對尺寸的第一和第三行:

tableLayoutPanel1.RowStyles[0].Height = 100; 
tableLayoutPanel1.RowStyles[0].SizeType = SizeType.Absolute; 
tableLayoutPanel1.RowStyles[2].Height = 100; 
tableLayoutPanel1.RowStyles[2].SizeType = SizeType.Absolute; 

要確保第二(中間行)應該有SizeType = SizeType.PercentHeight = 100。你Form應該有最大200 Height

1

我做

this.tableLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom))); 
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F)); 
    this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); 
    this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F)); 

,並通過設置錨頂部和底部我確信工作...... ,如果調整該行會越鬧越大/小,並通過使第一和第三排的絕對尺寸和中間百分比尺寸我確保只有中間會變大/變小

相關問題