Control.SizeChanged 事件


Size 属性值更改时发生。Occurs when the Size property value changes.

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




下面的代码示例演示了该 SizeChanged 事件。The following code example demonstrates the SizeChanged event. Button提供了控件的实例,可以水平和垂直缩放。An instance of a Button control has been provided that can be scaled both horizontally and vertically. NumericUpDown实例提供水平和垂直缩放值。A NumericUpDown instance provides the horizontal and vertical scale value. Button名为OK的实例用于设置控件实例的刻度值 ButtonThe Button instance named OK is used to set the scale values for the Button control instance. 每当控件大小发生更改时,就会调用与控件的事件关联的事件处理程序 SizeChangedWhenever the size of the control changes, the event handler associated with the SizeChanged event of the control is called. 此事件处理程序将显示一个消息框,指示控件的大小已更改。This event handler displays a message box indicating that the size of the control has changed.

   void RegisterEventHandler()
      myButton1->SizeChanged += gcnew EventHandler( this, &MyForm::MyButton1_SizeChanged );

   void MyButton2_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
      // Set the scale for the control to the value provided.
      float scale = (float)myNumericUpDown1->Value;
      myButton1->Scale( scale );

   void MyButton1_SizeChanged( Object^ /*sender*/, System::EventArgs^ /*e*/ )
      MessageBox::Show( "The size of the 'Button' control has changed" );
private void RegisterEventHandler()
   myButton1.SizeChanged += new EventHandler(this.MyButton1_SizeChanged);

private void MyButton2_Click(object sender, System.EventArgs e)
   // Set the scale for the control to the value provided.
   float scale = (float)myNumericUpDown1.Value;

private void MyButton1_SizeChanged(object sender, System.EventArgs e)
   MessageBox.Show("The size of the 'Button' control has changed");
Private Sub RegisterEventHandler()
   AddHandler myButton1.SizeChanged, AddressOf MyButton1_SizeChanged
End Sub

Private Sub MyButton2_Click(sender As Object, e As EventArgs) 
   ' Set the scale for the control to the value provided.
   Dim scale As Single = CSng(myNumericUpDown1.Value)
End Sub

Private Sub MyButton1_SizeChanged(sender As Object, e As EventArgs)
   MessageBox.Show("The size of the 'Button' control has changed")
End Sub


更可取的方法是使用 Layout 事件处理自定义布局。It is preferable to use the Layout event to handle custom layouts. Layout事件是为了响应 Resize 事件而引发的,但在其他情况下,可能需要应用布局。The Layout event is raised in response to Resize events, but also in other conditions when layout might need to be applied.

如果 Size 编程修改或用户交互更改了属性,则会引发此事件。This event is raised if the Size property is changed by either a programmatic modification or user interaction.

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