Table of contents
1.
Introduction
2.
Design-Time
3.
Run-Time
4.
Frequently Asked Questions
4.1.
What is the use of RadioButton control?
4.2.
What is RadioButton C#?
4.3.
What is the difference between a CheckBox control and a RadioButton control?
4.4.
How do we control group form?
4.5.
What is form control?
5.
Conclusion
Last Updated: Mar 27, 2024

C# RadioButton Control

Author Alok Pandey
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A radio button, also known as an option button, allows the user to choose one option from a list of options when paired with other RadioButton controls. When a user selects a radio button, it is checked, and all other radio buttons in that group are unchecked.

For example, choose your gender from the drop-down menu, so you'll have to choose between Male, Female, or Transgender. RadioButton is a class in C# that is found under System.Windows.Forms namespace. When you select any radio button in a set of radio buttons, you can display text, pictures, or both.

We can create a radio button by two methods

1. Design-Time

2. Run-Time

Recommended topics, Palindrome in C#, singleton design pattern in c#, and  Ienumerable vs Iqueryable

Design-Time

This is the simplest approach to constructing a RadioButton control. In this method, we create a radio button with the graphical interface of the visual studio.

Step:1- Visual Studio -> File -> New -> Project -> WindowsFormApp 

Step 2: Drag and drop the RadioButton control from the ToolBox onto the Windows form. According to your needs, a RadioButton control can be placed anywhere on the Windows form.

Step 3: Drag and drop the RadioButton control from the ToolBox onto the Windows form. According to your needs, a RadioButton control can be placed anywhere on the Windows form.

 

OUTPUT

 

Run-Time

It's a little more difficult than the previous way. The RadioButton class is used to construct a RadioButton programmatically in this method. The steps below demonstrate how to dynamically build a RadioButton.

 

Step 1: Create a radio button with the help of the RadioButton() constructor that is provided by the RadioButton class. 

// Creating radio button
RadioButton rbtn = new RadioButton();

 

Step 2: Once the RadioButton is created, set the properties of the RadioButton provided by the RadioButton class.

// Set the AutoSize property 
rbtn.AutoSize = true;

 

// Add text in RadioButton
rbtn.Text = "India";

 

// Set the location of the RadioButton
rbtn.Location = new Point(190, 75);

 

// Set Font property 
rbtn.Font = new Font("Roboto", 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(rbtn);

 

Let's understand this through code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; 

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

    private void RadioButton1_CheckedChanged(object sender,EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Label
       Label btnl = new Label();
       btnl.AutoSize = true;
       btnl.Location = new Point(190, 75);
       btnl.Text = "Select Your Country";
       btnl.Font = new Font("Roboto", 12);
       this.Controls.Add(l);
        
      //option button & its properties
        radiobutton btn1 = new radiobutton();
        btn1. Autosize = true;
        btn1. Text = "india";
        btn1. Location = new point(190, 100);
        btn1. Font = new font("roboto", 12);
        this. Controls. Add(btn1);
 
     //option button & its properties
        radiobutton btn2 = new radiobutton();
        btn2. Autosize = true;
        btn2. Text = "america";
        btn2. Location = new point(190, 125);
        btn2. Font = new font("roboto", 12);
        this. Controls. Add(btn2);

 

   //option button & its properties
        radiobutton btn3 = new radiobutton();
        btn3. Autosize = true;
        btn3. Text = "russia";
        btn3. Location = new point(190, 150);
        btn3. Font = new font("roboto", 12);
        this. Controls. Add(btn3);
    }
  }
}

 

OUTPUT


Frequently Asked Questions

What is the use of RadioButton control?

A radio button, often known as an option button, is a graphical control element that allows users to select only one option from a collection of mutually incompatible choices. A radio button is distinguished from checkboxes by its single feature, enabling the user to choose and deselect any number of items.

What is RadioButton C#?

RadioButton is a class in C# that is found under System.Windows.Forms namespace. When you select any radio button in a set of radio buttons, you can display text, pictures, or both.

What is the difference between a CheckBox control and a RadioButton control?

Selecting elements include checkboxes and radio buttons. Checkboxes allow users to select items from a limited number of options, whereas radio buttons allow users to select exactly one item from a list of multiple options.

How do we control group form?

By surrounding form controls within the fieldset element, they can be grouped. The controls of a fieldset are then linked together. The legend element, which offers a label or description for the group, must be the first element inside the fieldset.

What is form control?

A form control is a user interface control that acts as a conduit between the user and the server.

Conclusion

In this article, we have extensively discussed C# RadioButton Control.

We hope that this blog has helped you enhance your knowledge regarding  C# RadioButton Controls and if you would like to learn more, check out our articles on  C# Dynamic TypeC# ConstructorC# Functions.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 

Enroll in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass