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 Application using Visual Studio 2017. Radio Button allows Distinct Selection of Several items. In each group of RadioButtons, only one can be checked or Selected. Conceptually, this control implements an Exclusive Selection.CheckBoxes allow Multiple selection at once.

RADIOBUTTON CONTROL

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

STEP 1 - Start the Project

Lets get Started, Open visual studio 2017--->start new project ---->Windows Forms Application and name it as 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 as Red, Blue and Green.

Windows Forms

STEP 3 - Coding For Button Click Event

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

Windows Forms 

STEP 4 - Compile and Run-Sample output

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

Windows Forms 

STEP 5 - GroupBox Control

You can Add GroupBox Control or Panel control into 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 Nesting Strategy, you can have Multiple groups of RadioButtons on a Single Form.

Windows Forms 

STEP 6 - Coding 

Coding For the Button Click Event.

Windows Forms 
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     string scolor = "";  
  4.     if(radioButton1.Checked)  
  5.     {  
  6.         scolor = radioButton1.Text;  
  7.     }  
  8.   
  9.     if (radioButton2.Checked)  
  10.     {  
  11.         scolor = radioButton2.Text;  
  12.     }  
  13.   
  14.     if (radioButton3.Checked)  
  15.     {  
  16.         scolor = radioButton3.Text;  
  17.     }  
  18.     MessageBox.Show(scolor);  
  19. }  
  20.   
  21. private void button2_Click(object sender, EventArgs e)  
  22. {  
  23.     Getcolor(radioButton4);  
  24.     Getcolor(radioButton5);  
  25.     Getcolor(radioButton6);  
  26. }  
  27. private void Getcolor(RadioButton rdoButton)  
  28. {  
  29.     if (rdoButton.Checked)  
  30.     {  
  31.         MessageBox.Show(rdoButton.Text);  
  32.     }  
  33. }  
STEP 7 -  Sample Output

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

Windows Forms 

Summary

RadioButton is Similar to that of Checkbox the only Difference is in RadioButton We can Select only One Option But in the Checkbox We can Select Multiple Option. 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