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.
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.
Step 3. Now open the common controls and drag and drop the RadioButton on the form where we want to it be placed.
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.
Here, we can also add different labels and radio buttons and further change their properties.
Output:
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:
Properties
Property | Description |
---|
Appearance | This property is used to set a value determining the appearance of the RadioButton. |
AutoCheck | This 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. |
AutoSize | This property is used to set a value that indicates whether the RadioButton control resizes based on its contents. |
BackColor | This property is used to set the background colour of the RadioButton control. |
CheckAlign | This property is used to set the location of the check box portion of the RadioButton. |
Checked | This property is used to set a value indicating whether the RadioButton control is checked. |
Font | This property is used to set the font of the text displayed by the RadioButton control. |
ForeColor | This property is used to set the foreground colour of the RadioButton control. |
Location | This 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. |
Name | This property is used to set the name of the RadioButton control. |
Padding | This property is used to set padding within the RadioButton control. |
Text | This property is used to set the text associated with this RadioButton control. |
TextAlign | This property is used to set the alignment of the text on the RadioButton control. |
Visible | This property is used to set a value indicating whether the RadioButton control and all its child controls are displayed. |
Events
Event | Description |
---|
Click | This event occurs when the RadioButton control is clicked. |
CheckedChanged | This event occurs when the value of the Checked property changes. |
AppearanceChanged | This event occurs when the Appearance property value changes. |
DoubleClick | This event occurs when the user double-clicks the RadioButton control. |
Leave | This event occurs when the input focus leaves the RadioButton control. |
MouseClick | This event occurs when the RadioButton control is clicked by the mouse. |
MouseDoubleClick | This event occurs when the user double-clicks the RadioButton control with the mouse. |
MouseHover | This event occurs when the mouse pointer rests on the RadioButton control. |
MouseLeave | This event occurs when the mouse pointer leaves the RadioButton control. |
Similar Reads
How to set Text in the RadioButton in C#?
In Windows Forms, RadioButton control is used to select a single option among the group of the options. For example, select your gender from the given list, so you will choose only one option among three options like Male or Female or Transgender. In Windows Forms, you are allowed to add text in the
3 min read
How to set the Name of the RadioButton in C#?
In Windows Forms, RadioButton control is used to select a single option among the group of the options. For example, select your gender from the given list, so you will choose only one option among three options like Male or Female or Transgender. In Windows Forms, you are allowed to give a name to
3 min read
How to set the Size of the RadioButton in C#?
In Windows Forms, RadioButton control is used to select a single option among the group of the options. For example, select your gender from the given list, so you will choose only one option among three options like Male or Female or Transgender. In Windows Forms, you are allowed to adjust the size
3 min read
How to set the Font of the RadioButton in C#?
In Windows Forms, RadioButton control is used to select a single option among the group of the options. For example, select your gender from the given list, so you will choose only one option among three options like Male or Female or Transgender. In Windows Forms, you are allowed to set the font of
3 min read
Label in C#
In Windows Forms, Label control is used to display text on the form. It does not interact with user input or handle mouse or keyboard events. Labels are used to provide information to the user within the form such as description, message, or details. These are the key points about labels.Display Tex
5 min read
How to set the appearance of RadioButton in C#?
In Windows Forms, RadioButton control is used to select a single option among the group of the options. For example, select your gender from the given list, so you will choose only one option among three options like Male or Female or Transgender. In Windows Forms, you are allowed to set the appeara
3 min read
How to set the Padding of the RadioButton in C#?
In Windows Forms, RadioButton control is used to select a single option among the group of the options. For example, select your gender from the given list, so you will choose only one option among three options like Male or Female or Transgender. In Windows Forms, you are allowed to adjust the padd
3 min read
How to set the Location of the RadioButton in C#?
In Windows Forms, RadioButton control is used to select a single option among the group of the options. For example, select your gender from the given list, so you will choose only one option among three options like Male or Female or Transgender. In Windows Forms, you are allowed to adjust the loca
3 min read
How to set the RadioButton to Checked State in C#?
In Windows Forms, RadioButton control is used to select a single option among the group of the options. For example, select your gender from the given list, so you will choose only one option among three options like Male or Female or Transgender. In Windows Forms, you are allowed to set a value whi
3 min read
How to set the Visibility of the RadioButton in C#?
In Windows Forms, RadioButton control is used to select a single option among the group of the options. For example, select your gender from the given list, so you will choose only one option among three options like Male or Female or Transgender. In Windows Forms, you are allowed to adjust the visi
3 min read