Beginner 8 min read Control #2c:\users\ali pc\onedrive\desktop\csharpmasteryhub\csharpmasteryhub\pages\windowsforms\toolboxmastery\controls\checkbox.cshtml c:\users\ali pc\onedrive\desktop\csharpmasteryhub\csharpmasteryhub\pages\windowsforms\toolboxmastery\controls\checkbox.cshtml.cs>

CheckBox Control

The CheckBox control allows users to select or deselect an option. It's perfect for Yes/No, True/False, or On/Off choices.

What is a CheckBox?

A CheckBox is a control that displays a check mark when selected. Users can click it to toggle between checked and unchecked states. Unlike RadioButtons, CheckBoxes are independent - checking one doesn't uncheck others.

Common uses: "I agree to terms", "Remember me", "Enable notifications", "Show password", and other Yes/No options.

How to Use

  1. Drag and drop a CheckBox from the Toolbox onto your form.
  2. Rename it (e.g., chkAgree, chkRemember).
  3. Set the Text property to what users will see.
  4. Set the Checked property to true if you want it checked by default.
  5. Double-click to create a CheckedChanged event handler.

Code Example

// 1. Drag a CheckBox onto the form
// 2. Set properties in the designer:
//    - Name: chkAgree
//    - Text: I agree to the terms
//    - Checked: false

// 3. Double-click to create CheckedChanged event
private void chkAgree_CheckedChanged(object sender, EventArgs e)
{
    // Enable or disable a button based on the checkbox
    btnSubmit.Enabled = chkAgree.Checked;
}

// 4. Check the state in code
if (chkAgree.Checked)
{
    MessageBox.Show("User agreed to terms");
}
else
{
    MessageBox.Show("User did not agree");
}

Important Properties

Property Description Example
Text The text displayed next to the checkbox chkAgree.Text = "I agree";
Checked Whether the checkbox is checked chkAgree.Checked = true;
CheckState Can be Checked, Unchecked, or Indeterminate chkAgree.CheckState = CheckState.Indeterminate;
ThreeState Allows three states (checked, unchecked, indeterminate) chkAgree.ThreeState = true;
AutoCheck Whether the checkbox toggles automatically on click chkAgree.AutoCheck = false;
Appearance Normal or Button appearance chkAgree.Appearance = Appearance.Button;

Exercise

Task: Create a pizza order form with checkboxes for toppings.

  1. Create a new Windows Forms project.
  2. Add a Label: "Select your pizza toppings".
  3. Add CheckBox controls for:
    • Pepperoni
    • Mushrooms
    • Onions
    • Extra Cheese
    • Olives
  4. Add a Button: "Order Pizza".
  5. When clicked, show a message with all selected toppings.
  6. Add a "Select All" checkbox that checks/unchecks all toppings.
Hint: Use a list or StringBuilder to collect all checked items.
Key Takeaway

CheckBoxes are perfect for independent Yes/No choices. Use the Checked property to read the state and the CheckedChanged event to respond to changes.