通常我们在设置子控件的一些与外观、布局有关的属性时,比如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()是模式窗体,就时不能随便切换的那种,只有当打开的窗体关闭时才能操作父窗体。