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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17718405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:18:07  来源:igfitidea点击:

Automatically resize TableLayoutPanel row when window is resized

c#winformslayouttablelayoutpanel

提问by John NoCookies

I have a simple 1x3 TableLayoutPanel. I want to achieve a very simple thing: When the window is resized, resize the middle row and keep top and bottom ones the same. I tried doing the logical thing, which is setting rigid top and bottom row sizes and autosize for the middle row. Unfortunately, it's the bottom row that resizes.

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

All of the inner panels have Dock set to Fill and default anchors. What am I doing wrong?

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

采纳答案by Bolu

Change middle row to 100% percent, which will tell the system that middle row will fill any gap left. So change this (I believe its your designer.cs):

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

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

to:

到:

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

Check TableLayoutPanel.RowStylefrom MSDN:

TableLayoutPanel.RowStyle从 MSDN检查:

  1. Rows with RowStyle set to Absolute are considered first, and their fixed heights are allocated.
  2. Rows with RowStyle set to AutoSize are sized to their contents.
  3. Remaining space is divided among rows withRowStyle set to Percent.
  1. 首先考虑 RowStyle 设置为 Absolute 的行,并分配它们的固定高度。
  2. RowStyle 设置为 AutoSize 的行根据其内容调整大小。
  3. 剩余空间在行间分配,RowStyle 设置为 Percent。

回答by King King

Simply set absolute sizes for the first and third rows:

只需为第一行和第三行设置绝对大小:

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

To be sure the second (middle) row should have SizeType = SizeType.Percentand Height = 100. Your Formshould have maximum Heightof 200.

确保第二行(中间)应该有SizeType = SizeType.PercentHeight = 100。您Form最多应该有Height200 个。

回答by No Idea For Name

i did

我做了

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));

and it worked... by setting the anchor top and bottom i made sure that if resize the row will grow bigger/smaller, and by making the first and 3rd rows absolute size and the middle percentage size i made sure only the middle will grow bigger/smaller

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