When Panel.Size is updated after adding controls, when Panel.AutoSize = true?

I am creating a GUI in C # using WinForms.
I am trying to position software-generated panels one under the other. Since the contents of these panels may vary depending on their contents, I use Panel.AutoSize to allow WinForms to resize correctly.

The problem is this: if I use Panel.Height (or Panel.Size.Height ) right after filling Panel , the return value is always the default. Resizing occurs as I see when the application starts, but I just don’t know when.

Here is a simplified version of what I'm doing:

 this.SuspendLayout(); int yPos = 0; foreach (String entry in entries) { Panel panel = new Panel(); panel.SuspendLayout(); panel.AutoSize = true; panel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly; panel.BackColor = System.Drawing.SystemColors.Window; // Allows to see that the panel is resized for dispay panel.Location = new System.Drawing.Point(0, yPos); panel.Size = new System.Drawing.Size(this.Width, 0); this.Controls.Add(panel); Label label = new Label(); label.AutoSize = true; label.Location = new System.Drawing.Point(0, 0); label.MaximumSize = new System.Drawing.Size(panel.Width, 0); label.Text = entry; panel.Controls.Add(label); panel.ResumeLayout(false); panel.PerformLayout(); yPos += panel.Height; // When breaking here, panel.Height is worth 0 yPos += label.Height; // This works perfectly, label.Height was updated according to the text content when breaking at that point } this.ResumeLayout(false); this.PerformLayout(); 

So the real question is: how can I get an updated Panel.Size after adding controls to it to get its correct height value?

Note. I know that I can use the height of the TextBox , but I find it inelegant and inappropriate since there are more controls in the Panel in my actual code, and I need to use this panel height a few lines below.

+7
source share
1 answer

What I believe is that the size of the panel will be determined when you execute PerformLayout on your parent. You can make it work the way you want by moving the parent code of the SuspendLayout / ResumeLayout into a loop.

 int yPos = 0; foreach (String entry in entries) { this.SuspendLayout(); Panel panel = new Panel(); panel.SuspendLayout(); panel.AutoSize = true; panel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly; panel.BackColor = System.Drawing.SystemColors.Window; // Allows to see that the panel is resized for dispay panel.Location = new System.Drawing.Point(0, yPos); panel.Size = new System.Drawing.Size(this.Width, 0); this.Controls.Add(panel); Label label = new Label(); label.AutoSize = true; label.Location = new System.Drawing.Point(0, 0); label.MaximumSize = new System.Drawing.Size(panel.Width, 0); label.Text = entry; panel.Controls.Add(label); panel.ResumeLayout(true); this.ResumeLayout(true); yPos += panel.Height; // When breaking here, panel.Height is worth 0 //yPos += label.Height; // This works perfectly, label.Height was updated according to the text content when breaking at that point } this.PerformLayout(); 
+5
source

All Articles