Button Class
Definition
Represents a Windows button control.
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
public class Button : System.Windows.Forms.ButtonBase, System.Windows.Forms.IButtonControl
- Inheritance
- Attributes
- Implements
Inherited Members
System.ComponentModel.Component
System.MarshalByRefObject
System.Object
System.Windows.Forms.ButtonBase
System.Windows.Forms.Control
Examples
The following code example creates a Button, sets its DialogResult property to DialogResult, and adds it to a Form.
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 );
}
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);
}
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
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. The FlatStyle property can also be set to FlatStyle, 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.
Constructors
| Button() |
Initializes a new instance of the Button class. |
Properties
| AutoSizeMode |
Gets or sets the mode by which the Button automatically resizes itself. |
| CreateParams |
Gets a CreateParams on the base class when creating a window. |
| DialogResult |
Gets or sets a value that is returned to the parent form when the button is clicked. |
Methods
| NotifyDefault(Boolean) |
Notifies the Button whether it is the default button so that it can adjust its appearance accordingly. |
| OnClick(EventArgs) |
Raises the Click event. |
| OnFontChanged(EventArgs) |
Raises the FontChanged event. |
| OnMouseEnter(EventArgs) |
Raises the OnMouseEnter(EventArgs) event. |
| OnMouseLeave(EventArgs) |
Raises the OnMouseLeave(EventArgs) event. |
| OnMouseUp(MouseEventArgs) |
Raises the OnMouseUp(MouseEventArgs) event. |
| OnTextChanged(EventArgs) |
Raises the TextChanged event. |
| PerformClick() |
Generates a Click event for a button. |
| ProcessMnemonic(Char) |
Processes a mnemonic character. |
| ToString() |
Returns a String containing the name of the Component, if any. This method should not be overridden. |
| WndProc(Message) |
Processes Windows messages. |
Events
| DoubleClick |
Occurs when the user double-clicks the Button control. |
| MouseDoubleClick |
Occurs when the user double-clicks the Button control with the mouse. |