问题描述
我在 C#的使用的的WinForms 的。结果
我想定位programaticaly创建面板以下任一其他创建GUI。由于这些面板的内容可以根据其内容各不相同,我使用 Panel.AutoSize
让的WinForms执行正确的大小调整。
I'm creating a GUI in C# using WinForms.
I'm trying to position programaticaly created panels one below the other. As the content of these panel can vary depending on their content, I'm using Panel.AutoSize
to let WinForms perform the correct resizing.
现在的问题是:如果我使用 Panel.Height
(或 Panel.Size.Height
)填充面板
之后,返回值始终是我的默认值。 。调整大小确实出现,作为启动应用程序时,我所看到的,但我只是不知道什么时候
The problem is: if I'm using Panel.Height
(or Panel.Size.Height
) right after populating the Panel
, the value returned is always my default value. The resizing do occur, as I can see when launching the app, but I just don't know when.
下面是我在做什么的简化版本:
Here's 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();
所以,真正的问题是:我怎样才能得到更新的 Panel.Size ?
将控件添加到它之后,才能得到其应有的高度值
So the real question is: How can I get the updated Panel.Size
after adding controls to it, to get its proper height value?
的注:我知道我可以使用文本框
的高度,但我觉得不雅的和不切实际的,因为在我的实际代码有更多的控制在面板
,我需要用该面板高度以下几行。的
Note: I know I can use the TextBox
height, but I find it inelegant and impractical, as in my actual code there are more controls in the Panel
and I need to use that panel height a few lines below.
推荐答案
我beleive的情况是,该小组的规模将当你在它的父做PerformLayout确定。你可以把它的工作就像你是通过移动面板的父 SuspendLayout / ResumeLayout
代码到河套缺憾。
What I beleive is happening is that the Size of the Panel will be determined when you do PerformLayout on its Parent. You can make it work like you are wanting by moving the panel's parent SuspendLayout / ResumeLayout
code into the 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();
这篇关于当Panel.Size添加控件时Panel.AutoSize = TRUE后更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!