C# RadioButton
Hello folks, hope you are doing good. Today in this article I'm going to explain you all about C# RadioButton Control. In my previous articles, we discussed C# Checked ListBox & C# ListBox Control. C# RadioButton is also known as the OptionButton. The radio button allows the user to select the specific values as the input. When user will click on the radio button, it will get activated, a user can select only one radio button at the same time. Radio buttons have mostly used in the point of same applications and signup forms. You have mostly seen the radio button for the gender selection in the forms. Radio button makes easier for a user to give input. You can make specific input values. You can create any kind of application with the help of Radio Button. Even that you can create the polling desktop application by using a Radio button.

C# RadioButton Control

C# RadioButton has its own importance in desktop applications. Mostly point of sale applications used a Radio button to retrieve the specific input values. Such as the payment method is cash or credit. C# RadioButton enables the user to checked just one option at the same time and other options remain unchecked or unselected. If you are wanted to add C# RadioButton in your desktop application then you have to use the ToolBox in the design tab. Just search for the RadioButton and drag it to your application. In the following image, you can be observed that we have added the 8 RadioButtons and only one is selected. Because you can't select more than one RadioButton at the same time.
C# RadioButton
You also observed that the name of radio buttons are the default names. If you want to change their name then you have two options. The first option is to change the name from the property section which is at the right side of visual studio development environment. We have changed the name of the first column radio buttons by this method. In the following image, you can be observed how we changed the name of radio buttons.
C# RadioButton
The second method to change the name of a radio button is programmatically. In the source code, you have to set the radio button name with the help of Text property of radio button. After the name of radio button object call the text property and set the value. In the following code, you can be observed that how we set the value of radio button text property.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            radioButton5.Text = "TEP Code RB5";
            radioButton6.Text = "TEP Code RB6";
            radioButton7.Text = "TEP Code RB7";
            radioButton8.Text = "TEP Code RB8";
        }
    }
}
It depends on your ease, you can use the both method to set the name of radio buttons. For your better understanding, we have attached the screenshot of output along with the code.
C# RadioButton

C# RadioButton Properties

If you are wanted to override the default properties of C# RadioButton. Then you have to know about all properties,  such as you can change the color of radio button text, changed the size of text, a color of background and foreground etc. Supposed that you are wanted to change the state of RadioButton, by default every radio button is unchecked, you can be checked any radio button during the execution of a desktop application. In the following code, we have used the Checked property and make that true.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            radioButton1.Checked = true;
        }
    }
}
If you will add more radio buttons checked true then the only last radio button will be checked. Because you can only be checked one radio button at the same time. In the following image, you can preview that only radio button1 is checked.
C# RadioButton
If you are wanted to change the background color of any RadioButton then you have to use the BackColor property and set the color. There are varieties of color which you can use to set as the background color. In the following code, we have used different colors for each of the buttons.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace strnull
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            radioButton1.BackColor = Color.AliceBlue;
            radioButton2.BackColor = Color.AntiqueWhite;
            radioButton3.BackColor = Color.Aqua;
            radioButton4.BackColor = Color.Aquamarine;
            radioButton5.BackColor = Color.Azure;
            radioButton6.BackColor = Color.Beige;
            radioButton7.BackColor = Color.Bisque;
            radioButton8.BackColor = Color.BlueViolet;
        }
    }
}
You must have to use the Color object to set any color. For the better understanding, we have taken the screenshot of output along with the code which is overriding the color properties.
C# RadioButton
If you are wanted to change the forecolor or the color of the text then you have to use the ForeColor properties. It's same as the BackColor. In the following code, we have used the dark colors to dominate them on a windows form.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            radioButton1.ForeColor = Color.YellowGreen;
            radioButton2.ForeColor = Color.Tomato;
            radioButton3.ForeColor = Color.Violet;
            radioButton4.ForeColor = Color.Turquoise;
            radioButton5.ForeColor = Color.SteelBlue;
            radioButton6.ForeColor = Color.Sienna;
            radioButton7.ForeColor = Color.SeaGreen;
            radioButton8.ForeColor = Color.BlueViolet;
        }
    }
}
We used the Color object to set the color. In the following image, you can observe that how the color changes the appearance and interface. By using the color schemes you can make eye attractive windows form and make easier for the user to identified usable features of your app.
C# RadioButton
If you are wanted to change the font size for radio button text then you have to use the Font property. To set the size we have to pass the font family prototype and font size as the parameter in font constructor. In the following code, you can observe that how we changed the font size of a radio button.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            radioButton1.Font = new Font(Font.FontFamily,10);
            radioButton2.Font = new Font(Font.FontFamily, 11);
            radioButton3.Font = new Font(Font.FontFamily, 12);
            radioButton4.Font = new Font(Font.FontFamily, 13);
            radioButton5.Font = new Font(Font.FontFamily, 12);
            radioButton6.Font = new Font(Font.FontFamily, 11);
            radioButton7.Font = new Font(Font.FontFamily, 10);
            radioButton8.Font = new Font(Font.FontFamily, 9);
        }
    }
}
In the above code, we have used a different size for each RadioButton. The first four RadioButton size is increased and other four RadioButton size get a decrease. In the following image, you can observe the size of radio buttons.
C# RadioButton
If you are wanted to change the font family then you have to use the same font property which is used to change the font size. There are two parameters which we used to change the font family, the first parameter is the name of font and second parameter is the size of the text. Font constructor doesn't allow single parameter that's why we passed the size. In the following code, we have used the different fonts for each C# RadioButton.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            radioButton1.Font = new Font("Times New Roman", 11);
            radioButton2.Font = new Font("Century", 11);
            radioButton3.Font = new Font("Arial", 11);
            radioButton4.Font = new Font("Comic Sans MS", 11);
            radioButton5.Font = new Font("Copperplate Gothic Light", 11);
            radioButton6.Font = new Font("Georgia", 11);
            radioButton7.Font = new Font("Impact", 11);
            radioButton8.Font = new Font("Lucida Console", 11);
            
        }
    }
}
All the above-used fonts are the default font which is available in Windows. If you don't know which font you have to used then used Google Font. It will help you to select the best font for your desktop application. In the following image, you can observe how font changes the appearance of your desktop application.
C# RadioButton
If you are wanted to validate which of the Radio Button is selected as the option then you can use the Checked property after the name of RadioButton Object name. You have to use the If Conditional Statement to create the logic if radio button object is checked then show the popup message. In the following code, we have demonstrated the functionality. Each radio button contains a logical statement to return the value in the message box if it is checked.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent(); 
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                MessageBox.Show(radioButton1.Text);
            }
            else
            if (radioButton2.Checked == true)
            {
                MessageBox.Show(radioButton2.Text);
            }
            else
            if (radioButton3.Checked == true)
            {
                MessageBox.Show(radioButton3.Text);
            }
            else
            if (radioButton4.Checked == true)
            {
                MessageBox.Show(radioButton4.Text);
            }
            else
            if (radioButton5.Checked == true)
            {
                MessageBox.Show(radioButton5.Text);
            }
            else
            if (radioButton6.Checked == true)
            {
                MessageBox.Show(radioButton6.Text);
            }
            else
            if (radioButton7.Checked == true)
            {
                MessageBox.Show(radioButton7.Text);
            }
            else
            if (radioButton8.Checked == true)
            {
                MessageBox.Show(radioButton8.Text);
            }
        }
    }
}
If you want to try the above code in your own compiler, then you have to add button click event first then you can execute the above code. It will allow you to select an option, and when you press the button it will generate the popup message and return the value of selected radio button. In the following image, you can observe the output of the above code.
C# RadioButton
If you are wanted to use the image as the background for the radio button then you have to use the image property. We will be used the image property to set the path of an image as the background. Don't use the large image as the background because it will extend to screen automatically. In the following code, you can be observed how you can set any image as the background.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            radioButton1.Image = Image.FromFile("C:\\Users\\Jade\\Pictures\\brownImage.jpg");
        }  
    }
}

C# RadioButton Event

C# allows us to enhance the functionalities according to our requirements. Every desktop application has their own needs and uniqueness. If you want to perform any specific function on the certain time or on the certain action, then we used the C# Events. C# Events are simple as the other common functions, which are provided by the visual studio. You can use any user define a function and set it as the event handler. It depends on the developer ease. Mostly developer used the builtin events. There are several C# Events which are common among all the C# Tools. Some Events are specific to C# Tools, mean you can't use all the events for all the tools. Following are the C# Events which we will be used with RadioButton to enhance the interaction of the end-user.
  • C# RadioButton BackColorChanged Event
  • C# RadioButton CheckedChanged Event
  • C# RadioButton Click Event
  • C# RadioButton ForeColorChanged Event
  • C# RadioButton MouseHover Event
  • C# RadioButton MouseLeave Event
  • C# RadioButton TextChanged Event
We have already mentioned that there are several C# Events which you can use with RadioButton. We just used the above events with RadioButton to give the major concepts behind every event and logical usage.

C# RadioButton BackColorChanged Event

This event will occur only when the background color of a radio button is changed. Supposed we want to show a message or perform any functionality when background color of radio button changed, then we will use BackColorChanged Event. There is two method by which we can set this event for RadioButton. The first method is to activate this event from the property section of radio button via events. The second method is to create a user-defined function and set as the BackColorChanged Event in the event properties. In the following image, you can compare the both method.
First Method Second Method
C# RadioButton
C# RadioButton
C# RadioButton
C# RadioButton
In the following code, we have used the button and it clicks event. We set the backColor to AliceBlue. When user will click the button, background color set to the AliceBlue. You also observed there is BackColorChanged Event in which we have used just a message box to just demonstrate that something is performed.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace strnull
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            radioButton1.BackColor = Color.AliceBlue;
        }

        private void radioButton1_BackColorChanged(object sender, EventArgs e)
        {
            MessageBox.Show("BackColor is changed to " + radioButton1.BackColor.ToString());
        }
        
    }
}
In the above code, when the background color of the radio button is changed. Message prompt with the name of color which is set as the background. In the following image, you can observe the output of the above code.
C# RadioButton

C# RadioButton CheckedChanged Event

This event will occur and executed when user will change the option, such as user have select A first and then select the B then this event will be executed. In the following code, we are going to demonstrate the above scenario.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            MessageBox.Show("Option changed.");
        }
        
    }
}
In the above code, we have used two radio button. Whenever you will change the radio buttons value or option CheckedChanged event gets executed. In the below image you can observe the output
C# RadioButton

C# RadioButton Click Event

This event will occur when user will click on the radio button, whatever user will click to check the option or just click in the text of radio button. Supposed that you have required performing any kind of functionality when user will click on the radio button or the text of radio button then you can used Click event which will fulfill your requirement for this scenario. In the following code, we have demonstrated the above scenario.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           

        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("You Clicked!");
        }
        
    }
}
In the above code, we have used two radio buttons and just added the click event to the first radio button. If you will click the radio button1 then you will observe the popup message but if you clicked in the radio button2 then nothing will happen except that radio button2 get selected. It's because we have added the Click event just for the radio button1. In the following image, you can observe the output of above code.
C# RadioButton

C# RadioButton ForeColorChanged Event

This event gets executed when user will change the font color or fore color of the radio button. Supposed that you have created a button and set fore color changed the code in button click function. So that when user will click the button, fore color get changed and after this ForeColorChanged event performed on the changing of the fore color. In the following code, you can observe the above scenario.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           

        }

        private void button1_Click(object sender, EventArgs e)
        {
            radioButton1.ForeColor = Color.BlueViolet;

        }


        private void radioButton1_ForeColorChanged(object sender, EventArgs e)
        {
            MessageBox.Show("ForeColor Is changed to " + radioButton1.ForeColor.ToString());
        }
        
    }
}
In the above code, we have set the ForeColor ti BlueViolet when user will click the button. Then ForeColorChanged Event executed and prompt the message which color is selected as the ForeColor. In the following image, you can observe the output of the above code.
C# RadioButton

C# RadioButton MouseHover Event

This event is executed whenever the user hovers the cursor of the mouse on the radio button. If you will hover the mouse on radio button text then its also executed because that's also included in the boundaries of a radio button. In the following code, we are going to demonstrate the above scenario.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           

        }

        private void radioButton1_MouseHover(object sender, EventArgs e)
        {
            MessageBox.Show("Mouse Hover to " + radioButton1.Text.ToString());
        }
        
    }
}
In the above code, we have set the prompt message when user will hover the first radio button. You can declare any kind of functionality which you want to perform. In the following Image, you can be observed the output.
C# RadioButton

C# RadioButton MouseLeave Event

This event occurs when mouse cursor will leave the boundaries of the radio button, it's opposite to the mouseHover event. We are going to demonstrate this scenario in the following code along with the MouseHover Event. So that we can be observed both events at once.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace strnull
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void radioButton1_MouseHover(object sender, EventArgs e)
        {
            MessageBox.Show("Mouse Hover to " + radioButton1.Text.ToString());
        }

        private void radioButton1_MouseLeave(object sender, EventArgs e)
        {
            MessageBox.Show("Mouse Left " + radioButton1.Text.ToString());
        }
        
    }
}
In the above code, you cab observed the MouseHover and MouseLeave both events. When user will hover the mouse a prompt message show that user is hover mouse cursor on radiobutton1 and when a user left the radiobutton1 boundaries its show prompt message again. In the below image you can observe the output of above code.
C# RadioButton

C# RadioButton TextChanged Event

This event will execute when user will change the text. To perform its functionality we have to create a button click event in which we declared to change the radio button text. When user will click the button, a text of radio button is get changed and cause of change text, TextChanged Event get executed. In the following code, we have demonstrated the above scenario. We used the simple message box to pop the message when TextChanged Event gets executed.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            radioButton1.Text = "New Text";
        }

        private void radioButton1_TextChanged(object sender, EventArgs e)
        {
            MessageBox.Show("Radio Button Text changed to " + radioButton1.Text.ToString());
        }
        
    }
}
You can observe in the above code, there are two events. The button click event changed the text of radio button and TextChanged event get executed when text gets changed. In the following image, you can be observed the output. You also observed that when button event is executed radio button text doesn't change but the value is set to new text. After the execute of textChanged Event, it's get changed. It's because all the events have same priority or execution, so first events get executed then their effects applied.
C# RadioButton
We hope you will learn a lot of things from this C# Tutorial. We are looking forward from you about your experience with C# RadioButton. If you want to learn more on C# then you can subscribe our YouTube Channel and Playlist of C# Tutorials. We tried our best to provide you all possible solutions and concepts which you can use with C# RadioButton to enhance your desktop application development. Don't forget to share it with your friends. You can also check C# ListBox ControlC# Button ControlC# Label Control and C# TextBox Control.