Part One of my “Common .NET controls crash course” articles, “Common .NET Controls Crash Course, Part 1: Checkboxes,” spoke about the Checkbox control and how to use it. In Part 2, I am going to speak about the RadioButton control.
What Is a RadioButton?
A RadioButton control enables a user to select a single option from a group of choices. In other words, a RadioButton allows the distinct selection of several items. In a group of RadioButtons, only one can be selected at a time. This is known as exclusive selection.
This is shown in Figure 1.
Figure 1: Tabs Screen
You are allowed to choose only one option out of each group. When you do, the others are cleared. To create multiple groups on a form, place each RadioButton group inside its own container, such as a Panel or a GroupBox.
When a RadioButton is selected, it shows a black dot inside it. This can mean “yes” or “true” and, when nothing is selected, the RadioButton is empty; this can mean “no” or “false,” for example.
Using the RadioButton
Obviously, you need to first create a Windows Forms application and have at least one form. When the form has loaded, you can have a look in the Toolbox for the RadioButton. The Toolbox is shown in Figure 2.
Figure 2: Toolbox
Once the RadioButton has been added, you may notice the Properties window, as shown in Figure 3. This is where we set the RadioButton’s properties
Figure 3: Properties Window
Apart from the ordinary properties, like Name, Text, Enabled and so on, here are a few cool properties to look out for.
Cool RadioButton Properties
The following properties control how the RadioButton works and how it appears:
- CheckAlign
- AutoEllipsis
- AutoCheck
- Checked
CheckAlign
The CheckAlign RadioButton property gets or sets the location of the circle portion of the RadioButton.
The possible ContentAlignment values are:
- BottomCenter
- BottomLeft
- BottomRight
- MiddleCenter
- MiddleLeft
- MiddleRight
- TopCenter
- TopLeft
- TopRight
Figure 4 shows radioButton2 with a CheckAlign property of BottomRight
Figure 4: CheckAlign set to BottomRight
AutoEllipsis
The AutoEllipsis RadioButton property determines if the ellipsis character (…) should appear at the right edge of the RadioButton control, indicating that the text extends beyond the length of the control. For the ellipsis to appear, the RadioButton’s AutoSize property must be set to False.
Figure 5 shows what it looks like.
Figure 5: AutoEllipsis Property
AutoCheck
The RadioButton AutoCheck property determines if the RadioButton should be checked when clicked or not. If the AutoCheck property is set to false, the RadioButton’s Checked property should be set through code. This is to further ensure that only one RadioButton control is checked at a given time.
If you set the AutoCheck property to a RadioButton, and run the project, you will see that the check will not appear, because you need to set the Checked property explicitly through code.
Checked
The Checked property of a RadioButton determines if the RadioButton is checked or not.
When it is Checked, the RadioButton displays a circle. However, if the Appearance property is set to Button, it will appear raised or sunken as a toggle control.
You can use the Properties window to set the Checked property to true, or through code.
To set the Checked property using code, type the following code in a button’s click event, for example:
private void button1_Click(object sender, EventArgs e) { if (radioButton1.Checked) { MessageBox.Show("Checked"); radioButton2.Checked = false; } else { MessageBox.Show("Unchecked"); radioButton2.Checked = true; } }
This code checks if radioButton1 is Checked or not. Depending on whether or not radioButton1 is checked, radioButton2 gets checked and unchecked.
Conclusion
Part 2 focused on RadioButtons and how to use them productively. Part 3 focuses on ListBoxes.