6

我有一个简单的 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;

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

4

3 回答 3

11

将中间行更改为 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从 MSDN检查:

  1. 首先考虑 RowStyle 设置为 Absolute 的行,并分配它们的固定高度。
  2. RowStyle 设置为 AutoSize 的行根据其内容调整大小。
  3. 剩余空间在 RowStyle 设置为百分比的行之间划分。
于 2013-07-18T08:40:32.943 回答
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.Percentand Height = 100。您Form最多应该有Height200 个。

于 2013-07-18T08:42:18.780 回答
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));

它起作用了...通过设置锚顶部和底部,我确保如果调整大小,行会变大/变小,并通过使第一行和第三行绝对大小和中间百分比大小,我确保只有中间会增长更大/更小

于 2013-07-18T08:46:16.243 回答