Export (0) Print
Expand All
2 out of 3 rated this helpful - Rate this topic

Button Class

Represents a Windows button control.

For a list of all members of this type, see Button Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.Control
            System.Windows.Forms.ButtonBase
               System.Windows.Forms.Button

[Visual Basic]
Public Class Button
   Inherits ButtonBase
   Implements IButtonControl
[C#]
public class Button : ButtonBase, IButtonControl
[C++]
public __gc class Button : public ButtonBase, IButtonControl
[JScript]
public class Button extends ButtonBase implements IButtonControl

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.

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.

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.

Example

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

[C++] 
private:
    void InitializeMyButton() {
        // Create and initialize a Button.
        Button __gc *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);
    }; 

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

Requirements

Namespace: System.Windows.Forms

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

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

See Also

Button Members | System.Windows.Forms Namespace | ButtonBase | RadioButton | CheckBox | IButtonControl

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.