调整窗口大小时自动调整TableLayoutPanel行的大小
问题描述:
我有一个简单的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和默认锚点。我究竟做错了什么?
答
将中间行更改为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:
- 行与RowStyle设置为绝对的第一考虑,并且他们的固定高度被分配。
- RowStyle设置为AutoSize的行的大小根据其内容而定。
- 剩余空间在RowStyle设置为Percent的行之间分配。
答
只需设置绝对尺寸的第一和第三行:
tableLayoutPanel1.RowStyles[0].Height = 100;
tableLayoutPanel1.RowStyles[0].SizeType = SizeType.Absolute;
tableLayoutPanel1.RowStyles[2].Height = 100;
tableLayoutPanel1.RowStyles[2].SizeType = SizeType.Absolute;
要确保第二(中间行)应该有SizeType = SizeType.Percent
和Height = 100
。你Form
应该有最大200 Height
答
我做
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));
,并通过设置锚顶部和底部我确信工作...... ,如果调整该行会越闹越大/小,并通过使第一和第三排的绝对尺寸和中间百分比尺寸我确保只有中间会变大/变小