我正在使用WinForms在C#中创建GUI .
我试图将programaticaly创建的面板放在另一个下面.由于这些面板的内容可能会根据其内容而有所不同,我正在使用WinForms执行正确的大小调整.Panel.AutoSize
问题是:如果我在填充后立即使用Panel.Height(或Panel.Size.Height)Panel,则返回的值始终是我的默认值.调整大小确实发生了,正如我在启动应用程序时所看到的那样,但我只是不知道何时.
这是我正在做的简化版本:
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 …Run Code Online (Sandbox Code Playgroud)