Button Control
The Button is one of the most common controls in Windows Forms. It allows users to click to perform an action.
What is a Button?
A Button is a control that users click to trigger an action. When clicked, it fires a
Click event that you handle in your code. Buttons are used everywhere -
from "Submit" and "Cancel" to "Save" and "Delete".
How to Use
- Drag and drop a Button from the Toolbox onto your form.
- Rename it in the Properties panel (e.g.,
btnSave,btnCancel). - Set the Text property to what users will see (e.g., "Save", "Cancel").
- Double-click the button to create a Click event handler.
- Write code inside the event handler to perform the action.
Code Example
// 1. Drag a Button onto the form
// 2. Set properties in the designer:
// - Name: btnSave
// - Text: Save
// - BackColor: Green
// - ForeColor: White
// 3. Double-click the button to create the Click event
private void btnSave_Click(object sender, EventArgs e)
{
// Show a message when the button is clicked
MessageBox.Show("Data saved successfully!");
}
// 4. You can also enable/disable the button
private void btnSave_Enabled()
{
// Disable the button
btnSave.Enabled = false;
// Enable the button
btnSave.Enabled = true;
}
Important Properties
| Property | Description | Example |
|---|---|---|
Text |
The text displayed on the button | btnSave.Text = "Save"; |
Enabled |
Whether the button can be clicked | btnSave.Enabled = false; |
BackColor |
Background color of the button | btnSave.BackColor = Color.Green; |
ForeColor |
Text color of the button | btnSave.ForeColor = Color.White; |
Font |
Font style and size | btnSave.Font = new Font("Arial", 12); |
Image |
An image on the button | btnSave.Image = Image.FromFile("save.png"); |
DialogResult |
Sets the result when used in a dialog | btnCancel.DialogResult = DialogResult.Cancel; |
Common Events
| Event | Description |
|---|---|
Click |
Fires when the button is clicked (most common) |
MouseEnter |
Fires when the mouse hovers over the button |
MouseLeave |
Fires when the mouse leaves the button |
Exercise
Task: Create a simple counter application.
- Create a new Windows Forms project.
- Add a Label to display the count (start at 0).
-
Add three Buttons:
- One with Text "Increment" to increase the count
- One with Text "Decrement" to decrease the count
- One with Text "Reset" to reset the count to 0
- Write Click event handlers for each button.
- Add a fourth Button with Text "Disable" that disables all three buttons.
- Add a fifth Button with Text "Enable" that enables all three buttons.
int count = 0; at the class level to track the count.
Show Solution
public partial class MainForm : Form
{
private int _count = 0;
public MainForm()
{
InitializeComponent();
UpdateLabel();
}
private void btnIncrement_Click(object sender, EventArgs e)
{
_count++;
UpdateLabel();
}
private void btnDecrement_Click(object sender, EventArgs e)
{
_count--;
UpdateLabel();
}
private void btnReset_Click(object sender, EventArgs e)
{
_count = 0;
UpdateLabel();
}
private void btnDisable_Click(object sender, EventArgs e)
{
btnIncrement.Enabled = false;
btnDecrement.Enabled = false;
btnReset.Enabled = false;
}
private void btnEnable_Click(object sender, EventArgs e)
{
btnIncrement.Enabled = true;
btnDecrement.Enabled = true;
btnReset.Enabled = true;
}
private void UpdateLabel()
{
lblCount.Text = $"Count: {_count}";
}
}
Key Takeaway
Buttons are the most common way for users to trigger actions.
Always use meaningful names (e.g., btnSave, btnCancel)
and handle the Click event to perform the desired action.