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

C# 调整窗口大小时自动调整TableLayoutPanel行的大小,c#,winforms,layout,tablelayoutpanel,C#,Winforms,Layout,Tablelayoutpanel,我有一个简单的1x3表格布局面板。我想实现一个非常简单的事情:当窗口调整大小时,调整中间行的大小,并保持顶部和底部的大小相同。我试着做一件合乎逻辑的事情,即设置严格的顶行和底行大小,并自动调整中间行的大小。不幸的是,调整大小的是最下面一行 // // tableLayoutPanel1 // this.tableLayoutPanel1.ColumnCount = 1; this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Fo

我有一个简单的1x3
表格布局面板
。我想实现一个非常简单的事情:当窗口调整大小时,调整中间行的大小,并保持顶部和底部的大小相同。我试着做一件合乎逻辑的事情,即设置严格的顶行和底行大小,并自动调整中间行的大小。不幸的是,调整大小的是最下面一行

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

所有内部面板都将停靠设置为填充和默认锚定。我做错了什么

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

致:

检查:

  • 首先考虑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
    。您的
    表单
    的最大
    高度应为200。

    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));
    
    它成功了。。。 通过设置锚顶部和底部,我确保如果调整大小,行将变大/变小,通过设置第一行和第三行的绝对大小和中间百分比大小,我确保只有中间行将变大/变小

    tableLayoutPanel1.RowStyles[0].Height = 100;
    tableLayoutPanel1.RowStyles[0].SizeType = SizeType.Absolute;
    tableLayoutPanel1.RowStyles[2].Height = 100;
    tableLayoutPanel1.RowStyles[2].SizeType = SizeType.Absolute;
    
    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));