Control.Resize 事件

定义

在调整控件大小时发生。Occurs when the control is resized.

public:
 event EventHandler ^ Resize;
public event EventHandler Resize;
member this.Resize : EventHandler 
Public Custom Event Resize As EventHandler 

示例

下面的代码示例处理 FormResize 事件。The following code example handles the Resize event of a Form. 调整窗体的大小时,事件处理程序会确保窗体保持为方形(其 HeightWidth 保持相等)。When the form is resized, the event handler ensures that the form stays square (its Height and Width remain equal). 若要运行此示例,请确保并将此事件处理方法与窗体的 Resize 事件相关联。To run this example, make sure and associate this event-handling method with the form's Resize event.

private:
   void Form1_Resize( Object^ sender, System::EventArgs^ /*e*/ )
   {
      Control^ control = dynamic_cast<Control^>(sender);

      // Ensure the Form remains square (Height = Width).
      if ( control->Size.Height != control->Size.Width )
      {
         control->Size = System::Drawing::Size( control->Size.Width, control->Size.Width );
      }
   }
private void Form1_Resize(object sender, System.EventArgs e)
{
   Control control = (Control)sender;
        
   // Ensure the Form remains square (Height = Width).
   if(control.Size.Height != control.Size.Width)
   {
      control.Size = new Size(control.Size.Width, control.Size.Width);
   }
}
Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize

   Dim myControl As Control
   myControl = sender

   ' Ensure the Form remains square (Height = Width).
   If myControl.Size.Height <> myControl.Size.Width Then
      myControl.Size = New Size(myControl.Size.Width, myControl.Size.Width)
   End If
End Sub

注解

若要确定已调整大小的控件的 Size,可以将已注册 ControlEventHandler 方法的 sender 参数强制转换为 Control 并获取其 Size 属性(或分别 HeightWidth 属性)。To determine the Size of the resized control, you can cast the sender parameter of the registered ControlEventHandler method to a Control and get its Size property (or Height and Width properties individually).

若要处理自定义布局,请使用 Layout 事件,而不是 Resize 事件。To handle custom layouts, use the Layout event instead of the Resize event. Layout 事件是为了响应 Resize 事件而引发的,但也用于响应影响控件布局的其他更改。The Layout event is raised in response to a Resize event, but also in response to other changes that affect the layout of the control.

有关处理事件的详细信息,请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

适用于

另请参阅