通常我們在設定子控件的一些與外觀、布局有關的屬性時,比如Size、Location、Anchor 或 Dock等,會激發子控件的Layout事件,并可能會引起視窗重繪,當子控件較多時,如果頻繁設定上述屬性(例如在窗體的初始化代碼中),多個子控件的Layout事件會引起視窗重繪效率問題,比如閃爍。使用SuspendLayout(),其後的代碼中将會把子控件的Layout事件暫時挂起,隻是把相應屬性的值設定為新值,并不激發Layout事件,待調用ResumeLayout()後,再一起使子控件的Layout事件生效。
如:
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();
}
大量添加控件時會提高效率。
===================================
show()是非模式窗體
ShowDialog()是模式窗體,就時不能随便切換的那種,隻有當打開的窗體關閉時才能操作父窗體。