当调整控件的多个属性时,将先后使用 SuspendLayout 和 ResumeLayout 方法取消多个 Layout 事件。例如,通常先调用 SuspendLayout 方法,然后设置控件的 Size、Location、Anchor 或 Dock 属性,最后调用 ResumeLayout 方法以使更改生效。
private void AddButtons()
{
// Suspend the form layout and add two buttons.
this.SuspendLayout();//控件的布局逻辑被挂起,直到调用 ResumeLayout 方法为止。
Button buttonOK = new Button();
buttonOK.Location = new Point(10, 10);
buttonOK.Size = new Size(75, 25);
buttonOK.Text = "OK";
Button buttonCancel = new Button();
buttonCancel.Location = new Point(90, 10);
buttonCancel.Size = new Size(75, 25);
buttonCancel.Text = "Cancel";
this.Controls.AddRange(new Control[]{buttonOK, buttonCancel});
this.ResumeLayout();
}
大量添加控件时会提高效率
本文介绍了一种在添加大量控件时提高效率的方法:通过使用SuspendLayout和ResumeLayout方法来暂时挂起控件的布局逻辑。这种方法可以避免每次设置属性时触发不必要的布局更新,从而显著提升应用程序性能。
1256

被折叠的 条评论
为什么被折叠?



