System.Windows.Forms Namesp ...


.NET Framework Class Library
Button Class

Represents a Windows button control.

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

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
JScript
public class Button extends ButtonBase implements IButtonControl
Remarks

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.

NoteNote:

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.

Examples

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 );
   }
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);
 }

Inheritance Hierarchy

System..::.Object
  System..::.MarshalByRefObject
    System.ComponentModel..::.Component
      System.Windows.Forms..::.Control
        System.Windows.Forms..::.ButtonBase
          System.Windows.Forms..::.Button
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, 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.
Version Information

.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
See Also

Reference

Tags :


Page view tracker