Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Button Class
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
Button Class

Updated: November 2007

Represents a Windows button control, which reacts to the ButtonBase..::.Click event.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/xaml/presentation

Visual Basic (Declaration)
Public Class Button _
    Inherits ButtonBase
Visual Basic (Usage)
Dim instance As Button
C#
public class Button : ButtonBase
Visual C++
public ref class Button : public ButtonBase
J#
public class Button extends ButtonBase
JScript
public class Button extends ButtonBase
XAML Object Element Usage
<Button>
  Content
</Button>

The Button class inherits directly from the System.Windows.Controls.Primitives..::.ButtonBase class.

Content Model: Button is a ContentControl. Its content property is Content. For more information on the content model for Button, see Controls Content Model Overview.

Handle the ButtonBase..::.Click event to respond when the user clicks a Button.

The OnMouseLeftButtonDown method marks the MouseLeftButtonDown event as handled. To respond to the MouseLeftButtonDown event, 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 over the button.

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

C#
<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;
        }

More Code

How to: Create a Button That Has an Image This example shows how to include an image on a Button.
How to: Create a Control That Has an Access Key and Text Wrapping This example shows how to create a control that has an access key and supports text wrapping. The example uses a Label control to illustrate these concepts.
System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows.Media..::.Visual
        System.Windows..::.UIElement
          System.Windows..::.FrameworkElement
            System.Windows.Controls..::.Control
              System.Windows.Controls..::.ContentControl
                System.Windows.Controls.Primitives..::.ButtonBase
                  System.Windows.Controls..::.Button
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows Vista

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
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker