当前所在位置:珠峰网资料 >> 计算机 >> 软件水平 >> 正文
计算机高级软考:使用SuspendLayout和ResumeLayout
发布时间:2009/11/22 14:24:49 来源:城市学习网 编辑:admin
  使用 SuspendLayout 和 ResumeLayout
  许多 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();
  }
  [Visual Basic .NET]
  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 方法。
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved