“AutoSize=true”当标签变为多行时面板高度不会调整

问题描述 投票:0回答:1

我目前在 C# WinForms 应用程序中遇到面板问题。该面板停靠在表单顶部,AutoSize 设置为 True。在此面板中,我有一个 TableLayoutPanel,其 AutoSize 设置为 True 并填充对接。此 TableLayoutPanel 包含两行,每行都有一个填充该行的标签,并且也设置为 AutoSize。

当 TableLayoutPanel 中的标签变为多行时,就会出现问题。尽管将 TableLayoutPanel 及其行的 AutoSize 设置为 True,但当标签换行为多行时,面板的高度不会相应调整。

这是我的设置摘要:

面板停靠在表单顶部且 AutoSize=True。 TableLayoutPanel里面的面板用AutoSize=True和Fill对接。 TableLayoutPanel 中的两行,每行都包含一个 AutoSize=True 的标签。 但是,当 TableLayoutPanel 中的标签变为多行时,面板的高度保持静态,并且不会调整以适应多行标签。

我尝试了多种方法,包括调整面板和TableLayoutPanel的AutoSize和AutoSizeMode属性,但我一直无法解决问题。

当 TableLayoutPanel 中的标签变为多行时,是否有人对如何使面板动态调整其高度有任何见解或建议?

重现问题:

  1. 创建一个新的 C# WinForms 项目。
  2. 向项目添加一个空表单。
  3. 将设计者生成的表单代码替换为下面提供的代码片段。
  4. 更改表单的宽度,使标签自动换行并变为多行。
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            panel1 = new Panel();
            tableLayoutPanel1 = new TableLayoutPanel();
            label2 = new Label();
            label1 = new Label();
            panel1.SuspendLayout();
            tableLayoutPanel1.SuspendLayout();
            SuspendLayout();
            // 
            // panel1
            // 
            panel1.AutoSize = true;
            panel1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            panel1.Controls.Add(tableLayoutPanel1);
            panel1.Dock = DockStyle.Top;
            panel1.Location = new Point(100, 100);
            panel1.Name = "panel1";
            panel1.Padding = new Padding(3);
            panel1.Size = new Size(540, 98);
            panel1.TabIndex = 0;
            // 
            // tableLayoutPanel1
            // 
            tableLayoutPanel1.AutoSize = true;
            tableLayoutPanel1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            tableLayoutPanel1.BackColor = SystemColors.ControlDark;
            tableLayoutPanel1.ColumnCount = 1;
            tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
            tableLayoutPanel1.Controls.Add(label2, 0, 1);
            tableLayoutPanel1.Controls.Add(label1, 0, 0);
            tableLayoutPanel1.Dock = DockStyle.Fill;
            tableLayoutPanel1.Location = new Point(3, 3);
            tableLayoutPanel1.Name = "tableLayoutPanel1";
            tableLayoutPanel1.RowCount = 3;
            tableLayoutPanel1.RowStyles.Add(new RowStyle());
            tableLayoutPanel1.RowStyles.Add(new RowStyle());
            tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 50F));
            tableLayoutPanel1.Size = new Size(534, 92);
            tableLayoutPanel1.TabIndex = 0;
            // 
            // label2
            // 
            label2.AutoSize = true;
            label2.BackColor = Color.Chartreuse;
            label2.Dock = DockStyle.Fill;
            label2.Location = new Point(3, 24);
            label2.Margin = new Padding(3);
            label2.Name = "label2";
            label2.Size = new Size(528, 15);
            label2.TabIndex = 3;
            label2.Text = "asd asd asd as das das dasd as ddas";
            label2.TextAlign = ContentAlignment.MiddleCenter;
            // 
            // label1
            // 
            label1.AutoSize = true;
            label1.BackColor = Color.Coral;
            label1.Dock = DockStyle.Fill;
            label1.Location = new Point(3, 3);
            label1.Margin = new Padding(3);
            label1.Name = "label1";
            label1.Size = new Size(528, 15);
            label1.TabIndex = 2;
            label1.Text = "dasdasdsa";
            label1.TextAlign = ContentAlignment.MiddleCenter;
            // 
            // Form2
            // 
            AutoScaleDimensions = new SizeF(7F, 15F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(740, 525);
            Controls.Add(panel1);
            Name = "Form2";
            Padding = new Padding(100);
            Text = "Form2";
            panel1.ResumeLayout(false);
            panel1.PerformLayout();
            tableLayoutPanel1.ResumeLayout(false);
            tableLayoutPanel1.PerformLayout();
            ResumeLayout(false);
            PerformLayout();
        }

        #endregion

        private Panel panel1;
        private TableLayoutPanel tableLayoutPanel1;
        private Label label2;
        private Label label1;
    }
c# winforms controls tablelayoutpanel
1个回答
0
投票

不要使用

panel1
。相反,设置
tableLayoutPanel1.Dock = DockStyle.Top;
并将其直接添加到表单中。另外,删除第三行(这将删除空白的灰色区域)。


另一种解决方案是在调整大小事件处理程序中计算面板的高度并手动计算它们:

private void TableLayoutPanel1_Resize(object sender, EventArgs e)
{
    int height = 12;
    foreach (Control c in tableLayoutPanel1.Controls) {
        height += c.Height;
    }
    tableLayoutPanel1.Height = Math.Max(92, height);
    panel1.Height = tableLayoutPanel1.Height + 6;
}
© www.soinside.com 2019 - 2024. All rights reserved.