Export (0) Print
Expand All
Expand Minimize

Control.OnClick Method

Raises the Click event.

[Visual Basic]
Protected Overridable Sub OnClick( _
   ByVal e As EventArgs _
)
[C#]
protected virtual void OnClick(
 EventArgs e
);
[C++]
protected: virtual void OnClick(
 EventArgs* e
);
[JScript]
protected function OnClick(
   e : EventArgs
);

Parameters

e
An EventArgs that contains the event data.

Remarks

Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

The OnClick method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors:  When overriding OnClick in a derived class, be sure to call the base class's OnClick method so that registered delegates receive the event.

Example

[Visual Basic, C#] The following code example demonstrates overriding the OnClick method in a derived class. To run the example, paste the following code after a form class, in the same file. Add a textbox of type SingleClickTextBox to the form.

[Visual Basic] 
' 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

[C#] 
// 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);
    }


}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Control Class | Control Members | System.Windows.Forms Namespace | Click

Show:
© 2014 Microsoft