最初,我有同样的疑问,SuspendLayout
并且ResumeLayout
确实有效。然后我自己尝试并创建了一个示例应用程序,并在以后更好地了解了这个概念。
所以,这就是我所做的:
mainPanel.SuspendLayout()
create child control
call child.SuspendLayout()
change the child control properties
add the child control to the mainPanel
call child.ResumeLayout(false) - this means: next layout run, relayout this control, but not immediately
repeat (2-6) for every child-control
call mainPanel.ResumeLayout(true) - this means: relayout my mainPanel and every child-control now!
也证明我的概念这里是示例应用程序
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
this.SuspendLayout();
for (int i = 0; i < 2000; i++)
{
var textbox = new TextBox();
//textbox.SuspendLayout();
//textbox.Dock = i% 2 ==0 ? DockStyle.Left : DockStyle.Right;
textbox.Dock = DockStyle.Fill;
textbox.Top = i * 10;
textbox.Text = i.ToString();
this.Controls.Add(textbox);
//textbox.ResumeLayout(false);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",ts.Hours, ts.Minutes, ts.Seconds,ts.Milliseconds / 10);
this.ResumeLayout(true);
MessageBox.Show(elapsedTime);