Working With Radio Button Control In Winforms Application Using Visual Studio 2017

Introduction

In this article, I am going to explain how to work with Radio Button Control in Windows Forms Applications using Visual Studio 2017. Radio Button allows a Distinct Selection of Several items. In each group of radio buttons, only one can be checked or Selected. Conceptually, this control implements an Exclusive Selection.CheckBoxes allow Multiple selections at once.

RadioButton Control

The RadioButton Provides a User Interface for an Exclusive Selection.RadioButton or option Buttons enable the user to select a single option from a group of choices when Paired with other Radio Buttons. When a user clicks on the Radio Button, it Becomes Checked and all other radio Buttons with the Same group become Unchecked.

Step 1. Start the Project

Let's get Started, Open Visual Studio 2017--->start new project ---->Windows Forms Application and name it RadioButton Control.

Windows Forms

Step 2. Drag and Drop Control

By default, the Designer Page will be loaded now Drag and Drop the Radio Button control and Button from the Toolbox into the Form Application in Visual Studio 2017. Change the Text of the Radio Button to Red, Blue, and Green.

Designer Page

Step 3. Coding For Button Click Event

Follow the code given below in the screenshot. When one option is chosen from the group of options of Radio Buttons and then the Button is clicked, the Selected option will be displayed in the Message Box.

Code

Step 4. Compile and Run-Sample output

Compile and Run the code the Following Output will be obtained as given below in the Screenshot.

 Run the code

Step 5. GroupBox Control

You can Add GroupBox Control or Panel control to the Form. In this Control, you can Nest RadioButton Instances. Drag and Drop GroupBox and inside it Drag and Drop Radio Button Control and Button Control. By using the Nesting Strategy, you can have Multiple groups of RadioButtons on a Single Form.

GroupBox Control

Step 6. Coding

Coding For the Button Click Event.

Click Event

private void button1_Click(object sender, EventArgs e)
{
    string scolor = "";
    if (radioButton1.Checked)
    {
        scolor = radioButton1.Text;
    }
    if (radioButton2.Checked)
    {
        scolor = radioButton2.Text;
    }
    if (radioButton3.Checked)
    {
        scolor = radioButton3.Text;
    }
    MessageBox.Show(scolor);
}
private void button2_Click(object sender, EventArgs e)
{
    Getcolor(radioButton4);
    Getcolor(radioButton5);
    Getcolor(radioButton6);
}
private void Getcolor(RadioButton rdoButton)
{
    if (rdoButton.Checked)
    {
        MessageBox.Show(rdoButton.Text);
    }
}

Step 7. Sample Output

Compile and Run the code. The following Output will be obtained as given below in the Screenshot. When One Option is Chosen from Group of Options of RadioButtons then the Selected Option will be displayed in the Message Box When the Button is Clicked.

Message Box

Summary

RadioButton is Similar to that Checkbox the only Difference is in RadioButton We can Select only One Option But in the Checkbox We can Select Multiple Options. Thanks For Reading My article.If there is any error or Problem with the Code feel free to comment it below. I will try to solve the problem.


Similar Articles