C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
In each group of RadioButtons, only one can be checked or selected. Conceptually, this control implements an exclusive selection. CheckBoxes allow multiple selections at once.
Steps. To begin, you should make a new Windows Forms application in Visual Studio, and then you can add GroupBox (or Panel) controls to it. In these controls, you can nest RadioButton instances.
Tip: By using this nesting strategy, you can have multiple groups of RadioButtons on a single form.
Tip 2: You may want to add the CheckedChanged event handler on your RadioButtons.
Also: You can actually have all the RadioButtons point to the same CheckedChanged event handler using the event window in Visual Studio.
Example RadioButton program: C# using System; using System.Windows.Forms; namespace WindowsFormsApplication11 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { // Executed when any radio button is changed. // ... It is wired up to every single radio button. // Search for first radio button in GroupBox. string result1 = null; foreach (Control control in this.groupBox1.Controls) { if (control is RadioButton) { RadioButton radio = control as RadioButton; if (radio.Checked) { result1 = radio.Text; } } } // Search second GroupBox. string result2 = null; foreach (Control control in this.groupBox2.Controls) { if (control is RadioButton) { RadioButton radio = control as RadioButton; if (radio.Checked) { result2 = radio.Text; } } } this.Text = result1 + " " + result2; } } }
In the example, we wired up all six RadioButtons to the radioButton1_CheckedChanged event handler. Thus, whenever the user clicks or changes the selection on the form, the above method will be executed.
The radioButton1_CheckedChanged event handler implements some custom logic to update the title bar text whenever the user changes the selection of RadioButtons. We loop over each sub-control of groupBox1 and groupBox2.
And: When we encounter a RadioButton, we access the Checked property and then acquire the Text property.
Finally: We set the title text of the Window to our findings. This is to show the program correctly works.
Groups. In Windows Forms, the RadioButton controls are grouped according to their enclosing control. To create multiple groups of RadioButtons, you would need to add containers such as Panel or GroupBox and then put RadioButtons inside.
RadioButton, CheckBox. So when should you use a RadioButton as opposed to a CheckBox? If only one option can be logically selected, then you should use a RadioButton. If more than one (or zero) options can be selected, you should use the CheckBox.
Note: The example screenshot is only correct if the user can only have one Pet and only have one Home.
Also: For animal lovers or people who are wealthy and own many homes, the dialog would not be sufficient.
Summary. The RadioButton provides a user interface for an exclusive selection. We looked at how to use event handlers on RadioButtons. We also saw how to imperatively search RadioButtons for the checked one.
And: We discussed, conceptually, the use of RadioButtons and also their grouping behavior.