Control.SuspendLayout Method


临时挂起控件的布局逻辑。Temporarily suspends the layout logic for the control.

 void SuspendLayout();
public void SuspendLayout ();
member this.SuspendLayout : unit -> unit
Public Sub SuspendLayout ()


下面的代码示例将两个按钮添加到窗体。The following code example adds two buttons to a form. 该示例通过使用 SuspendLayoutResumeLayout 方法来交易添加按钮。The example transactions the addition of the buttons by using the SuspendLayout and ResumeLayout methods.

   void AddButtons()
      // Suspend the form layout and add two buttons.
      Button^ buttonOK = gcnew Button;
      buttonOK->Location = Point(10,10);
      buttonOK->Size = System::Drawing::Size( 75, 25 );
      buttonOK->Text = "OK";
      Button^ buttonCancel = gcnew Button;
      buttonCancel->Location = Point(90,10);
      buttonCancel->Size = System::Drawing::Size( 75, 25 );
      buttonCancel->Text = "Cancel";
      array<Control^>^temp5 = {buttonOK,buttonCancel};
      this->Controls->AddRange( temp5 );
private void AddButtons()
   // Suspend the form layout and add two buttons.
   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});
Private Sub AddButtons()
   ' Suspend the form layout and add two buttons.
   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})
End Sub


控件的布局逻辑将挂起,直到调用 ResumeLayout 方法。The layout logic of the control is suspended until the ResumeLayout method is called.

当你调整控件的多个属性时,将使用 SuspendLayoutResumeLayout 方法来取消多个 Layout 事件。The SuspendLayout and ResumeLayout methods are used in tandem to suppress multiple Layout events while you adjust multiple attributes of the control. 例如,通常会调用 SuspendLayout 方法,然后设置控件的 SizeLocationAnchorDock 属性,然后调用 ResumeLayout 方法以使更改生效。For example, you would typically call the SuspendLayout method, then set the Size, Location, Anchor, or Dock properties of the control, and then call the ResumeLayout method to enable the changes to take effect.

若要成功调用 ResumeLayout,必须没有挂起的调用 SuspendLayoutThere must be no pending calls to SuspendLayout for ResumeLayout to be successfully called.


向父控件添加几个控件时,建议您在初始化要添加的控件之前调用 SuspendLayout 方法。When adding several controls to a parent control, it is recommended that you call the SuspendLayout method before initializing the controls to be added. 向父控件添加控件后,调用 ResumeLayout 方法。After adding the controls to the parent control, call the ResumeLayout method. 这将提高具有许多控件的应用程序的性能。This will increase the performance of applications with many controls.

Applies to

See also