发表于: other | 作者: | 日期: 2013/5/13 04:05

许多 Windows 窗体控件(例如,ListView 和 TreeView 控件)都实现了 SuspendLayout 和 ResumeLayout 方法,它们能够防止控件在添加子控件时创建多个布局事件。如果您的控件以编程方式添加和删除子控件或者执行动态布局,则您应该调用 SuspendLayout 和 ResumeLayout 方法。通过 SuspendLayout 方法,可以在控件上执行多个操作,而不必为每个更改执行布局。例如,如果您调整控件的大小并移动控件,则每个操作都将引发单独的布局事件。这些方法按照与 BeginUpdate 和 EndUpdate 方法类似的方式操作,并且在性能和用户界面稳定性方面提供相同的好处。下面的示例以编程方式向父窗体中添加按钮:
C#代码

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 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();
}

VB代码

Private Sub AddButtons()
‘ Suspend the form layout and add two buttons
Me.SuspendLayout()
Dim buttonOK As New Button
buttonOK.Location = New Point(10, 10)
buttonOK.Size = New Size(75, 25)
buttonOK.Text = “OK”
Dim buttonCancel As New Button
buttonCancel.Location = New Point(90, 10)
buttonCancel.Size = New Size(75, 25)
buttonCancel.Text = “Cancel”
Me.Controls.AddRange(New Control() { buttonOK, buttonCancel } )
Me.ResumeLayout()
End Sub

每当您添加或删除控件、执行子控件的自动布局或者设置任何影响控件布局的属性(例如,大小、位置、定位点或停靠属性)时,您都应该使用 SuspendLayout 和 ResumeLayout 方法。

: https://blog.darkmi.com/2013/05/13/3233.html

本文相关评论 - 1条评论都没有呢
Post a comment now » 本文目前不可评论

No comments yet.

Sorry, the comment form is closed at this time.