HOWTO · Csharp
How to Disable and Enable Buttons in C#
Disable and re-enable buttons in Windows Forms, WPF, and ASP.NET with the correct state property and event-handler patterns.
On this page
To disable a button in a Windows Forms app, set its Enabled property to false; set it to true to make the button available again. The exact property depends on the UI framework: WPF uses IsEnabled, while Windows Forms and ASP.NET Web Forms use Enabled. (C#)
What the Button State Property Does
An enabled button can receive user interaction and invoke its action. Disabling it prevents the user from activating it through the interface, and frameworks usually show that state visually. A button starts enabled unless the application, designer, or markup sets another initial value. Disabling a control is an interface behavior, not a substitute for checking permissions or validating data in the operation that handles a request.
Create or Select a Windows Forms Button
In the Windows Forms designer, add a Button from the Toolbox and set its (Name) property to myButton. The designer-created control is available to the form’s code, so event handlers can update it. You can also create a Button in code and add it to the form; ButtonBase is a base type, not the concrete control normally added for a button.
Disable and Re-enable a Windows Forms Button
Put state-changing code in the form class, where myButton is available. This Windows Forms example assumes the designer has created myButton and a separate resetButton on the same form. Add these handlers inside the form class, then connect myButton.Click to DisableButton_Click and resetButton.Click to EnableButton_Click in the designer’s Events pane (or with myButton.Click += DisableButton_Click; and resetButton.Click += EnableButton_Click; after InitializeComponent()): private void DisableButton_Click(object sender, EventArgs e) { myButton.Enabled = false; } and private void EnableButton_Click(object sender, EventArgs e) { myButton.Enabled = true; }. Clicking the first button disables that same button; clicking the separate reset button restores it. These handler snippets are Windows Forms-specific and are not standalone programs.
The following self-contained console example illustrates the state change only. It does not create a Windows Forms control; its output shows the Boolean state before and after the assignments. Use the Button.Enabled assignments above in a Windows Forms form.
using System;
class Program
{
static void Main()
{
bool submitButtonEnabled = true;
submitButtonEnabled = false;
Console.WriteLine($"Enabled: {submitButtonEnabled}");
submitButtonEnabled = true;
Console.WriteLine($"Enabled: {submitButtonEnabled}");
}
}
Enabled: False
Enabled: True
Use Button State in Event Handlers and Form Validation
A common pattern is to keep a Submit button unavailable until required fields pass validation. After a field changes, validate the current values and set submitButton.Enabled to the validation result. In a form-level validation handler, set it back to true only when all required checks succeed. Also validate again when processing submission: UI state helps guide the user but should not be the only guard around an operation. For a long-running request, disable Submit while work is in progress and restore it when the operation finishes, including failure paths, so users can retry.
Framework-Specific Property Names
Use the property belonging to the control type: Windows Forms Button.Enabled, WPF Button.IsEnabled, and ASP.NET Web Forms Button.Enabled. In WPF, IsEnabled can also be bound to a view-model value, allowing the interface to follow application state. ASP.NET Web Forms server-side Enabled controls the rendered server control; it is not a security check, and a client-side disabled state can be changed by a user. These snippets describe framework APIs and are not standalone programs.
Common Button-State Mistakes and Checks
- Check the framework and control type before choosing
EnabledorIsEnabled. - Confirm the handler is connected to the expected
Clickor validation event, and that the code refers to the intended button instance. - If a button stays disabled, trace every assignment to its state property and verify the validation condition can become true.
- Provide another route to restore a button disabled by its own click handler, such as completion of a task or a separate Cancel/Reset action.
- Enforce authorization and validate submitted values in the operation itself; a disabled appearance alone does not protect it.