调整窗口大小时自动调整TableLayoutPanel行的大小

Joh*_*ies 6 c# layout tablelayoutpanel winforms

我有一个简单的1x3 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;
Run Code Online (Sandbox Code Playgroud)

所有内部面板都将Dock设置为Fill和默认锚点.我究竟做错了什么?

Bol*_*olu 9

将中间行更改为100%,这将告诉系统中间行将填补任何剩余空白.所以改变这个(我相信它是你的designer.cs):

this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
Run Code Online (Sandbox Code Playgroud)

至:

this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
Run Code Online (Sandbox Code Playgroud)

TableLayoutPanel.RowStyle从MSDN检查:

  1. 将RowStyle设置为Absolute的行首先考虑,并分配其固定高度.
  2. RowStyle设置为AutoSize的行的大小与其内容相同.
  3. 剩余空间在行之间划分,其中RowStyle设置为Percent.