Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
ButtonBase Class
ButtonBase Events
 Click Event

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ButtonBase..::.Click Event

Occurs when a Button is clicked.

Namespace:  System.Windows.Controls.Primitives
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/xaml/presentation
Visual Basic (Declaration)
Public Event Click As RoutedEventHandler
Visual Basic (Usage)
Dim instance As ButtonBase
Dim handler As RoutedEventHandler

AddHandler instance.Click, handler
C#
public event RoutedEventHandler Click
Visual C++
public:
 event RoutedEventHandler^ Click {
    void add (RoutedEventHandler^ value);
    void remove (RoutedEventHandler^ value);
}
JScript
JScript does not support events.
XAML Attribute Usage
<object Click="RoutedEventHandler" .../>

Identifier field

ClickEvent

Routing strategy

Bubbling

Delegate

RoutedEventHandler

The user can raise the Click event by using an AccessText or by pressing ENTER or the SPACEBAR when the control has focus. When the user presses the SPACEBAR, the control sets IsPressed to true and captures the mouse. As a result, the control will raise mouse events such as MouseEnter and IsMouseDirectlyOverChanged. Note that using the AccessText or ENTER does not change IsPressed or capture the mouse, but it does raise the Click event.

The ButtonBase marks the MouseLeftButtonDown event as handled in the OnMouseLeftButtonDown method and raises the Click event. Hence, the OnMouseLeftButtonDown event will never occur for a control that inherits from ButtonBase. Instead, attach an event handler to the PreviewMouseLeftButtonDown event, or call AddHandler(RoutedEvent, Delegate, Boolean) with handledEventsToo set to true.

The following example shows three buttons that respond to clicks in three different ways.

  • Hover - the first button changes colors when the user hovers with the mouse over the button

  • Press - the second button requires that the mouse be pressed while the mouse pointer is in the button.

  • Release - the third does not reset the background color of the buttons until the mouse is pressed and released in the button.

<Button Name="btn1" Background="Pink" 
        BorderBrush="Black" BorderThickness="1" 
        Click="OnClick1" ClickMode="Hover">
  ClickMe1
</Button>

<Button Name="btn2" Background="LightBlue" 
        BorderBrush="Black" BorderThickness="1" 
        Click="OnClick2" ClickMode="Press">
  ClickMe2
</Button>

<Button Name="btn3" 
        Click="OnClick3" ClickMode="Release">
  Reset
</Button>

Visual Basic
Private Sub OnClick1(ByVal sender As Object, ByVal e As RoutedEventArgs)
    btn1.Background = Brushes.LightBlue
End Sub

Private Sub OnClick2(ByVal sender As Object, ByVal e As RoutedEventArgs)
    btn2.Background = Brushes.Pink
End Sub

Private Sub OnClick3(ByVal sender As Object, ByVal e As RoutedEventArgs)
    btn1.Background = Brushes.Pink
    btn2.Background = Brushes.LightBlue
End Sub

C#
        void OnClick1(object sender, RoutedEventArgs e)
        {
            btn1.Background = Brushes.LightBlue;
        }

        void OnClick2(object sender, RoutedEventArgs e)
        {
            btn2.Background = Brushes.Pink;
        }

        void OnClick3(object sender, RoutedEventArgs e)
        {
            btn1.Background = Brushes.Pink;
            btn2.Background = Brushes.LightBlue;
        }

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker