Freigeben über


Control.OnClick-Methode

Löst das Click-Ereignis aus.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Protected Overridable Sub OnClick ( _
    e As EventArgs _
)
'Usage
Dim e As EventArgs

Me.OnClick(e)
protected virtual void OnClick (
    EventArgs e
)
protected:
virtual void OnClick (
    EventArgs^ e
)
protected void OnClick (
    EventArgs e
)
protected function OnClick (
    e : EventArgs
)

Parameter

  • e
    Eine Instanz von EventArgs, die die Ereignisdaten enthält.

Hinweise

Durch das Auslösen eines Ereignisses wird der Ereignishandler über einen Delegaten aufgerufen. Weitere Informationen finden Sie unter Auslösen eines Ereignisses.

Die OnClick-Methode ermöglicht es auch abgeleiteten Klassen, das Ereignis ohne Anfügen eines Delegaten zu behandeln. Dies ist das bevorzugte Verfahren für die Behandlung des Ereignisses in einer abgeleiteten Klasse.

Hinweise für Erben Wenn Sie OnClick in einer abgeleiteten Klasse überschreiben, müssen Sie die OnClick-Methode der Basisklasse aufrufen, sodass registrierte Delegaten das Ereignis empfangen.

Beispiel

Im folgenden Beispiel wird das Überschreiben der OnClick-Methode in einer abgeleiteten Klasse veranschaulicht. Fügen Sie zum Ausführen dieses Beispiels den folgenden Code in derselben Datei hinter einer Formularklasse ein. Fügen Sie im Formular ein Textfeld vom Typ SingleClickTextBox hinzu.

' 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
// 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 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 extends TextBox
{
    protected void OnClick(EventArgs e)
    {
        this.SelectAll();
        super.OnClick(e);
    } //OnClick
} //SingleClickTextBox 

Im folgenden Codebeispiel wird eine der vielen Verwendungen des Click-Ereignisses und des Ereignishandlers veranschaulicht.

' 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
// 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 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.get_Parent();

    // Set the text and backcolor of the parent control.
    control.set_Text("My Groupbox");
    control.set_BackColor(Color.get_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.set_Text("The Form of My Control");
    myForm.set_BackColor(Color.get_Red());
} //button1_Click

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Control-Klasse
Control-Member
System.Windows.Forms-Namespace
Click