Open In App

RadioButton in C#

Last Updated : 20 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Windows Forms, RadioButton control is a Graphical User Interface (GUI) used to select a single option among the group of options. For example, select a gender from the given list, so choose only one option among three options like Male or Female or Others. When we select a radio button any previously selected radio will be automatically deselected from the same group.

In Windows Forms, The radio button is part of the System.Windows.Forms namespace. These are the common key features of the radio button:

Ways to Create a Radio Button In Windows Forms

There are mainly two ways to create a radio button in Windows forms which are mentioned below.

  • Drag and drop ( Design-Time)
  • Custom Button ( Run-Time)

Drag and drop ( Design-Time)

This is the easiest way to create a radio button in Windows Forms using Visual Studio we just have to open the toolbox and drag and drop the button on the form in the designer and further we can change the appearance of the radio button using the properties. Follow these steps to create a radio button.

Step 1: Now locate the project with the name here we are using the default name which is Form1 and it will open a form in the editor that we can further modify.

Empth-forms

In the image, we have two files that are open one Design and there is Form1.cs these two play a major role. We use the Form 1.cs file for the custom logic.


Step 2: Now open a Toolbox go to the view > Toolbox or ctrl + alt + x.

ToolBox


Step 3. Now open the common controls and drag and drop the RadioButton on the form where we want to it be placed.

RadioButton


Step 4. Now open the properties of the button press right-click on the button and it will open the properties solution explorer now we can change the button name to Click here.

Properties


Here, we can also add different labels and radio buttons and further change their properties.

btn


Output:

Form1-2025-03-20-14-38-41


Custom Button (Run Time)

In this method, we are going to modify the Form1.cs file and add custom code modification in C# with the help of the RadioButton class. The following steps show how to create a RadioButton dynamically:

Step 1: Create a radio button using the RadioButton() constructor provided by the RadioButton class.

// Creating radio button

RadioButton r1 = new RadioButton();

Step 2: After creating the RadioButton, set the properties of the RadioButton provided by the RadioButton class.

// Set the AutoSize property

r1.AutoSize = true;


// Add text in RadioButton

r1.Text = "Intern";


// Set the location of the RadioButton

r1.Location = new Point(286, 40);


// Set Font property

r1.Font = new Font("Berlin Sans FB", 12);

Step 3: And last add this RadioButton control to the form using Add() method.

// Add this radio button to the form

this.Controls.Add(r1);

Step 4: Now double-click on the button in Design and it will open the Form cs file where code is written in C#. Here the program file is Form 1.cs Now write this code in Form1.cs file

Form1.cs file:

C#
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            // Creating and setting label
            Label l = new Label();
            l.AutoSize = true;
            l.Location = new Point(176, 40);
            l.Text = "Select Post:";
            l.Font = new Font("Berlin Sans FB", 12);

            // Adding this label to the form
            this.Controls.Add(l);

            // Creating and setting the 
            // properties of the RadioButton
            RadioButton r1 = new RadioButton();
            r1.AutoSize = true;
            r1.Text = "Intern";
            r1.Location = new Point(286, 40);
            r1.Font = new Font("Berlin Sans FB", 12);

            // Adding this label to the form
            this.Controls.Add(r1);

            // Creating and setting the 
            // properties of the RadioButton
            RadioButton r2 = new RadioButton();
            r2.AutoSize = true;
            r2.Text = "Team Leader";
            r2.Location = new Point(356, 40);
            r2.Font = new Font("Berlin Sans FB", 12);
            // Adding this label to the form
            this.Controls.Add(r2);

            // Creating and setting the 
            // properties of the RadioButton
            RadioButton r3 = new RadioButton();
            r3.AutoSize = true;
            r3.Text = "Software Engineer";
            r3.Location = new Point(480, 40);
            r3.Font = new Font("Berlin Sans FB", 12);

            // Adding this label to the form
            this.Controls.Add(r3);

        }

    }
}

Output:

OutputUsingCode


Properties

PropertyDescription
AppearanceThis property is used to set a value determining the appearance of the RadioButton.
AutoCheckThis property is used to set a value indicating whether the Checked value and the appearance of the RadioButton control automatically change when the RadioButton control is clicked.
AutoSizeThis property is used to set a value that indicates whether the RadioButton control resizes based on its contents.
BackColorThis property is used to set the background colour of the RadioButton control.
CheckAlignThis property is used to set the location of the check box portion of the RadioButton.
CheckedThis property is used to set a value indicating whether the RadioButton control is checked.
FontThis property is used to set the font of the text displayed by the RadioButton control.
ForeColorThis property is used to set the foreground colour of the RadioButton control.
LocationThis property is used to set the coordinates of the upper-left corner of the RadioButton control relative to the upper-left corner of its form.
NameThis property is used to set the name of the RadioButton control.
PaddingThis property is used to set padding within the RadioButton control.
TextThis property is used to set the text associated with this RadioButton control.
TextAlignThis property is used to set the alignment of the text on the RadioButton control.
VisibleThis property is used to set a value indicating whether the RadioButton control and all its child controls are displayed.

Events

EventDescription
ClickThis event occurs when the RadioButton control is clicked.
CheckedChangedThis event occurs when the value of the Checked property changes.
AppearanceChangedThis event occurs when the Appearance property value changes.
DoubleClickThis event occurs when the user double-clicks the RadioButton control.
LeaveThis event occurs when the input focus leaves the RadioButton control.
MouseClickThis event occurs when the RadioButton control is clicked by the mouse.
MouseDoubleClickThis event occurs when the user double-clicks the RadioButton control with the mouse.
MouseHoverThis event occurs when the mouse pointer rests on the RadioButton control.
MouseLeaveThis event occurs when the mouse pointer leaves the RadioButton control.

Next Article

Similar Reads