Home » C# Basics » 08 - Windows Programming - 1
8

RadioButton Control

Defines the properties of the radio-button control in a windows application

RadioButton controls provide a way to select an option among multiple mutually exclusive options i.e. we can select any one of the given options. RadioButton controls have properties and events similar to the CheckBox control and they are placed inside a GroupBox control as discussed earlier.

Here is a simple application to demonstrate the working of the CheckBox and RadioButton controls:

1. Start a new Windows Application Project in the Visual C# IDE.

2. Drag and Drop a GroupBox control on the form. Then Drag and drop three CheckBox controls inside the GroupBox control. Next, drag and drop another GroupBox control, just below the first one. Place four RadioButton controls in this second GroupBox control. Add a Button control inside this second GroupBox. Use the table to set the properties of the form and the controls:

Property

Value

Form

Text

StartPosition

MaximizeBox

FormBorderStyle

Second Application

Center Screen

False

FixedSingle

groupBox1

Text

Form Elements

groupBox2

Text

Form Background Color

checkBox1

(Name)

Text

Checked

cbMaximize

Maximize Button

False

checkBox2

(Name)

Text

Checked

cbMinimize

Minimize Button

True

checkBox3

(Name)

Text

Checked

cbControl

Control Box

True

radioButton1

(Name)

Text

rbRed

Red

radioButton2

(Name)

Text

rbBlue

Blue

radioButton3

(Name)

Text

rbGreen

Green

radioButton4

(Name)

Text

rbYellow

Yellow

button1

(Name)

Text

btnApply

Apply

The form should look like this:

The purpose of this application is to enable or disable form elements (minimize button, maximize button and control box) as user checks or un-checks the related checkboxes. Similarly, the background color of the form will change depending upon the color radio buttons selected by the user.

3. Double click on the first checkbox control. Its CheckedChanged event handler will open up in the code view. Type the following code inside the event handler:

private void cbMaximize_CheckedChanged(object sender, EventArgs e) { if (this.MaximizeBox == false) this.MaximizeBox = true; else this.MaximizeBox = false; }

We have used the `if' statement to check if the form has the maximize button or not. The keyword `this' refers to the form itself. Hence, this.MaximizeBox refers to the maximize button of the form.

Remember that while setting the properties of the form, we have set the MaximizeBox property to `false'. Therefore, when the application runs, the maximize button is disabled. In this condition, if the user checks the checkbox, the CheckedChanged event will occur. The `if' statement will check if the maximize button is not available (this.MaximizeBox==false). Since this condition is true (because the maximize button is not available initially), the statement inside `if' will execute and set the MaximizeBox property of the form to `true'. This will make the maximize button available on the form.

If the user un-checks the checkbox, once again the CheckedChanged event will occur. This time, however, the `if' condition will evaluate to `false' since the maximize button is available (due to the previous CheckedChanged event). Hence, the `else' part will execute, assigning the MaximizeBox property of the form with the value `false'. This will disable the maximize button.

Every time the user checks or un-checks the checkbox, the CheckedChanged event will take place. The code will check if the value of the MaximizeBox property is true or false and assign the opposite to it.

Similar code is used for the CheckedChanged event of the other two chaeckboxes:

private void cbMinimize_CheckedChanged(object sender, EventArgs e) { if (this.MinimizeBox == true) this.MinimizeBox = false; else this.MinimizeBox = true; } private void cbControl_CheckedChanged(object sender, EventArgs e) { if (this.ControlBox == true) this.ControlBox = false; else this.ControlBox = true; }

Remember that when the Control box is not visible, maximize, minimize and the close buttons will disappear as well.

4. Double click on the `Apply' button to open its Click event. Type the following code for the event:

private void btnApply_Click(object sender, EventArgs e) { if (rbRed.Checked == true) this.BackColor = Color.Red; else if (rbBlue.Checked == true) this.BackColor = Color.Blue; else if (rbGreen.Checked == true) this.BackColor = Color.Green; else if(rbYellow.Checked==true) this.BackColor = Color.Yellow; }

The code here is simple. We are checking which radio button is checked (selected by the user) and applying the corresponding color to the background of the form.

Save and run the application. Check and un-check the different checkboxes to show/hide form elements and select different radio buttons to apply background colors to the form.