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.
- Add a GroupBox: "Size".
- Add RadioButtons: Small, Medium, Large, Extra Large.
- Add a GroupBox: "Crust".
- Add RadioButtons: Thin, Thick, Stuffed.
- Add a GroupBox: "Payment".
- Add RadioButtons: Cash, Credit Card, Debit Card.
- 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.