RadioButton Control in C#

The RadioButton component lets you force a user to make a single selection from a set of choices. This component must be used in a group of at least two RadioButton instances. Only one member of the group can be selected at any given time. Now, we will create a RadioButtons that when chose it will change the background color of the Form. So, now let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application. 2. Next, add three RadioButton named RadioButton1 labeled "Yellow", RadioButton2 labeled "Blue", and RadioButton3 labeled "Red". Then add a Button named Button1. You must design your interface like this: design 3. Now, put this code in Button1_Click. This will display the result of changing the color of our form when choosing the preferred color in Radio Buttons.
  1. private void Button1_Click(System.Object sender, System.EventArgs e)
  2. {
  3.         if (RadioButton1.Checked == true)
  4.         {
  5.                 this.BackColor = Color.Yellow;
  6.                 return;
  7.         }
  8.         else if (RadioButton2.Checked == true)
  9.         {
  10.                 this.BackColor = Color.Blue;
  11.                 return;
  12.         }
  13.         else
  14.         {
  15.                 this.BackColor = Color.Red;
  16.         }
  17. }
We use the code statement of If-else Decision statement to choose from the three different choices. RadioButton1 labeled "Yellow", RadioButton2 labeled "Blue", and RadioButton3 labeled "Red". The checked = True here is our event for our RadioButton as it toggles on and off. When we check RadioButton1, it will have a background color of Yellow ( this.BackColor = Color.Yellow. The "this" here indicates that we are referring to Form1. Next, When we check RadioButton2, it will have a background color of Blue ( this.BackColor = Color.Blue. Otherwise, it will have a background color of Red.

Output:

output output output Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment