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.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class Button _
    Inherits ButtonBase _
    Implements IButtonControl
Visual Basic (Usage)
Dim instance As Button
C#
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
public class Button : ButtonBase, IButtonControl
Visual C++
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)]
public ref class Button : public ButtonBase, 
    IButtonControl
J#
/** @attribute ComVisibleAttribute(true) */
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */
public class Button extends ButtonBase implements IButtonControl
JScript
public class Button extends ButtonBase implements IButtonControl

A Button can be clicked by using the mouse, ENTER key, or SPACEBAR if the button has focus.

Set the AcceptButton or CancelButton property of a Form to allow users to click a button by pressing the ENTER or ESC keys even if the button does not have focus. This gives the form the behavior of a dialog box.

When you display a form using the ShowDialog method, you can use the DialogResult property of a button to specify the return value of ShowDialog.

You can change the button's appearance. For example, to make it appear flat for a Web look, set the FlatStyle property to FlatStyle..::.Flat. The FlatStyle property can also be set to FlatStyle..::.Popup, which appears flat until the mouse pointer passes over the button; then the button takes on the standard Windows button appearance.

Note:

If the control that has focus accepts and processes the ENTER key press, the Button does not process it. For example, if a multiline TextBox or another button has focus, that control processes the ENTER key press instead of the accept button.

The following code example creates a Button, sets its DialogResult property to DialogResult..::.OK, and adds it to a Form.

Visual Basic
Private Sub InitializeMyButton()
    ' Create and initialize a Button.
    Dim button1 As New Button()

    ' Set the button to return a value of OK when clicked.
    button1.DialogResult = DialogResult.OK

    ' Add the button to the form.
    Controls.Add(button1)
End Sub 'InitializeMyButton

C#
private void InitializeMyButton()
 {
    // Create and initialize a Button.
    Button button1 = new Button();

    // Set the button to return a value of OK when clicked.
    button1.DialogResult = DialogResult.OK;

    // Add the button to the form.
    Controls.Add(button1);
 }


Visual C++
private:
   void InitializeMyButton()
   {
      // Create and initialize a Button.
      Button^ button1 = gcnew Button;

      // Set the button to return a value of OK when clicked.
      button1->DialogResult = ::DialogResult::OK;

      // Add the button to the form.
      Controls->Add( button1 );
   }

J#
private void InitializeMyButton()
{
    // Create and initialize a Button.
    Button button1 = new Button();

    // Set the button to return a value of OK when clicked.
    button1.set_DialogResult(get_DialogResult().OK);

    // Add the button to the form.
    get_Controls().Add(button1);
} //InitializeMyButton

JScript
private function InitializeMyButton()
 {
    // Create and initialize a Button.
    var button1 : Button = new Button();

    // Set the button to return a value of OK when clicked.
    button1.DialogResult = System.Windows.Forms.DialogResult.OK;

    // Add the button to the form.
    Controls.Add(button1);
 }


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, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Pocket PC

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, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.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