我有一个简单的 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 和默认 anchor 。我究竟做错了什么?
最佳答案
将中间行更改为 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 :
- Rows with RowStyle set to Absolute are considered first, and their fixed heights are allocated.
- Rows with RowStyle set to AutoSize are sized to their contents.
- Remaining space is divided among rows withRowStyle set to Percent.
关于c# - 调整窗口大小时自动调整 TableLayoutPanel 行的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17718405/