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

标签 c# winforms layout tablelayoutpanel

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

  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.

关于c# - 调整窗口大小时自动调整 TableLayoutPanel 行的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17718405/

相关文章:

c# - 鼠标 "buffering"点击禁用按钮

c# - .NET Windows窗体自定义布局引擎

android - "Canvas: trying to draw too large bitmap"当Android N Display Size 设置为大于Small 时

c# - 在 C# 中增加 WCF Web 服务的超时

c# - 在c#中检测点击脱离控件

c# - 是否有理由在 C# 中始终声明类型后缀为 'f' 的 float ?

java - 如何防止 View 重叠?

zend-framework - 将变量发送到 Zend Framework 中的布局

c# - 从 CRM 加载数据的性能问题

c# - 如何在 C# WPF 中的 MS 访问数据库中存储来自 2 个不同表的数据的数据库中创建表