利用SuspendLayout 和 ResumeLayout 方法加速添加控件

當調整控件的多個屬性時,將先後使用 SuspendLayoutResumeLayout 方法取消多個 Layout 事件。例如,通常先調用 SuspendLayout 方法,然後設置控件的 SizeLocationAnchorDock 屬性,最後調用 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();
}

大量添加控件時會提高效率

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章