Control.OnClick(EventArgs) 메서드

정의

Click 이벤트를 발생시킵니다.

protected:
 virtual void OnClick(EventArgs ^ e);
protected virtual void OnClick (EventArgs e);
abstract member OnClick : EventArgs -> unit
override this.OnClick : EventArgs -> unit
Protected Overridable Sub OnClick (e As EventArgs)

매개 변수

e
EventArgs

이벤트 데이터가 포함된 EventArgs입니다.

예제

다음 코드 예제에서는 재정의를 보여 줍니다는 OnClick 파생된 클래스에서 메서드입니다. 예제를 실행하려면 양식 클래스 다음에 다음 코드를 같은 파일에 붙여넣습니다. 형식 SingleClickTextBox 의 텍스트 상자를 양식에 추가합니다.

// This is a custom TextBox control that overrides the OnClick method
// to allow one-click selection of the text in the text box.
public ref class SingleClickTextBox: public TextBox
{
protected:
   virtual void OnClick( EventArgs^ e ) override
   {
      this->SelectAll();
      TextBox::OnClick( e );
   }
};
// This is a custom TextBox control that overrides the OnClick method
// to allow one-click selection of the text in the text box.

public class SingleClickTextBox: TextBox

{
    protected override void OnClick(EventArgs e)
    {
        this.SelectAll();
        base.OnClick(e);
    }
}
' This is a custom TextBox control that overrides the OnClick method
' to allow one-click selection of the text in the text box.

Public Class SingleClickTextBox
    Inherits TextBox

    Protected Overrides Sub OnClick(ByVal e As EventArgs)
        Me.SelectAll()
        MyBase.OnClick(e)
    End Sub


End Class

다음 코드 예제에서는 이벤트 및 이벤트 처리기의 여러 사용 Click 중 하나를 보여 있습니다.

   // This example uses the Parent property and the Find method of Control to set
   // properties on the parent control of a Button and its Form. The example assumes
   // that a Button control named button1 is located within a GroupBox control. The 
   // example also assumes that the Click event of the Button control is connected to
   // the event handler method defined in the example.
private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Get the control the Button control is located in. In this case a GroupBox.
      Control^ control = button1->Parent;
      
      // Set the text and backcolor of the parent control.
      control->Text = "My Groupbox";
      control->BackColor = Color::Blue;
      
      // Get the form that the Button control is contained within.
      Form^ myForm = button1->FindForm();
      
      // Set the text and color of the form containing the Button.
      myForm->Text = "The Form of My Control";
      myForm->BackColor = Color::Red;
   }
// This example uses the Parent property and the Find method of Control to set
// properties on the parent control of a Button and its Form. The example assumes
// that a Button control named button1 is located within a GroupBox control. The 
// example also assumes that the Click event of the Button control is connected to
// the event handler method defined in the example.
private void button1_Click(object sender, System.EventArgs e)
{
   // Get the control the Button control is located in. In this case a GroupBox.
   Control control = button1.Parent;
   // Set the text and backcolor of the parent control.
   control.Text = "My Groupbox";
   control.BackColor = Color.Blue;
   // Get the form that the Button control is contained within.
   Form myForm = button1.FindForm();
   // Set the text and color of the form containing the Button.
   myForm.Text = "The Form of My Control";
   myForm.BackColor = Color.Red;
}
' This example uses the Parent property and the Find method of Control to set
' properties on the parent control of a Button and its Form. The example assumes
' that a Button control named button1 is located within a GroupBox control. The 
' example also assumes that the Click event of the Button control is connected to
' the event handler method defined in the example.
Private Sub button1_Click(sender As Object, e As System.EventArgs) Handles button1.Click
   ' Get the control the Button control is located in. In this case a GroupBox.
   Dim control As Control = button1.Parent
   ' Set the text and backcolor of the parent control.
   control.Text = "My Groupbox"
   control.BackColor = Color.Blue
   ' Get the form that the Button control is contained within.
   Dim myForm As Form = button1.FindForm()
   ' Set the text and color of the form containing the Button.
   myForm.Text = "The Form of My Control"
   myForm.BackColor = Color.Red
End Sub

설명

이벤트가 발생하면 대리자를 통해 이벤트 처리기가 호출됩니다. 자세한 내용은 이벤트 처리 및 발생합니다.

또한 OnClick 메서드를 사용하면 파생 클래스가 대리자를 연결하지 않고도 이벤트를 처리할 수 있습니다. 이는 파생 클래스에서 이벤트를 처리하는 기본 방법입니다.

상속자 참고

파생 클래스에서 OnClick(EventArgs)를 재정의하는 경우 등록된 대리자가 이벤트를 받도록 기본 클래스의 OnClick(EventArgs) 메서드를 호출해야 합니다.

적용 대상

추가 정보