Beginner 8 min read Control #21

RadioButton Control

RadioButtons allow users to select one option from a group. When one is selected, others in the same group are automatically deselected.

What is a RadioButton?

RadioButtons are used for mutually exclusive choices. Only one RadioButton in a group can be selected at a time. They're perfect for yes/no decisions, choosing options, and making selections.

Code Example

// RadioButtons in the same container are automatically grouped
// Put them in a GroupBox to group them

// Check which RadioButton is selected
private void btnSubmit_Click(object sender, EventArgs e)
{
    string selected;
    
    if (rbOption1.Checked)
        selected = "Option 1";
    else if (rbOption2.Checked)
        selected = "Option 2";
    else
        selected = "None";
    
    MessageBox.Show($"Selected: {selected}");
}

// Set initial selection
rbOption1.Checked = true;

Exercise

Task: Create a pizza order form with RadioButtons.

  1. Add a GroupBox: "Size".
  2. Add RadioButtons: Small, Medium, Large, Extra Large.
  3. Add a GroupBox: "Crust".
  4. Add RadioButtons: Thin, Thick, Stuffed.
  5. Add a GroupBox: "Payment".
  6. Add RadioButtons: Cash, Credit Card, Debit Card.
  7. Add a Button to show all selections.
Key Takeaway

RadioButtons are for mutually exclusive choices. Group them using a GroupBox or Panel. Use the Checked property to check if selected.