C# WinForms中AutoSize=true的Panel在标签多行时高度不调整求助
C# WinForms中Panel无法随多行Label自动调整高度的问题
我在C# WinForms应用中遇到如下问题:将Panel停靠在窗体顶部且设置AutoSize=true,其内部包含一个设为AutoSize=true并填充停靠的TableLayoutPanel,该控件有两行,每行均有一个设为AutoSize=true的Label。当Label文本换行变为多行时,Panel的高度并未随之自动调整。
我的设置详情:
- Panel停靠于窗体顶部,
AutoSize=true; - Panel内的TableLayoutPanel设为
AutoSize=true,停靠方式为Fill; - TableLayoutPanel的两行各包含一个
AutoSize=true的Label。
但当Label变为多行时,Panel高度保持固定,无法容纳多行Label。我尝试调整Panel和TableLayoutPanel的AutoSize、AutoSizeMode属性,仍未能解决问题。
请问有谁能提供思路或建议,让Panel在Label变为多行时动态调整高度?
复现步骤:
- 创建新的C# WinForms项目;
- 向项目添加空白窗体;
- 将窗体的设计器生成代码替换为以下代码片段;
- 调整窗体宽度使Label文本换行变为多行。
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; }
内容的提问来源于stack exchange,提问作者Micra
相关产品推荐
相关产品推荐

