在Designer中会看到这两个方法,究竟是做什么用的呢?
一般在加载所有控件之前SuspendLayout方法,挂起所有控件在界面的逻辑布局事件,等又有布局代码完后再调用 ResumeLayout事件吧刚才挂起的逻辑布局一并执行。
例如我们在设置初始化控件时候,都会用到Size、Location、Anchor 或 Dock属性,而每一个属性都会去改变控件在界面上的布局,使得控件,每执行一段代码就就要界面重绘一次,那么我们先挂起所有事件,之后一起执行,这样就可以提高带有许多控件的应用程序的性能。
上文地址:http://hi.baidu.com/zouqiner/blog/item/a6118b4aa92391f982025ce9
下文地址:http://www.cnblogs.com/badnewfish/archive/2007/01/19/624298.html
临时挂起控件的布局逻辑。
命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)
语法:
C#
public void SuspendLayout ()
备注:
控件的布局逻辑被挂起,直到调用 ResumeLayout 方法为止。
当调整控件的多个属性时,将先后使用 SuspendLayout 和 ResumeLayout 方法取消多个 Layout 事件。例如,通常先调用 SuspendLayout 方法,然后设置控件的 Size、Location、Anchor 或 Dock 属性,最后调用ResumeLayout 方法以使更改生效。
SuspendLayout 调用必须等于零,然后才能成功调用 ResumeLayout。
注意
将多个控件添加到父控件时,建议在初始化要添加的控件之前调用 SuspendLayout 方法。将控件添加到父控件之后,调用 ResumeLayout 方法。这样就可以提高带有许多控件的应用程序的性能。
示例
下面的代码示例向窗体添加两个按钮(3个哦,其中一个是w)。该示例通过使用 SuspendLayout 和 ResumeLayout方法进行添加按钮。
private void AddButtons()
{
// Suspend the form layout and add two buttons.
this.SuspendLayout();
Button buttonOK = new Button();
buttonOK.Location = new Point(10, 10);
buttonOK.Size = new Size(75, 25);
buttonOK.Text = "OK";
Button button1 = new Button();
// Set the button to return a value of OK when clicked.
button1.DialogResult = DialogResult.OK;
// Add the button to the form.
Controls.Add(button1);
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();
}