Simple C# examples to help you learn about the RadioButton components in C#.
This tutorial will help you learn the following concepts:
- How to create and use RadioButtons in
Windows Forms
programmatically. - How to add several controls to a RadioButton
- How to set text property of a radiobutton
- How to autocheck as well as check a radiobutton programmatically.
- How to handle click events of a radiobutton
- How to set custom font to a radiobutton
- How to handle
CheckedChanged
events of a RadioButton.
Example 1: Simple RadioButtons
This example will comprise the following files:
RadioButton.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project.
Step 2: Write Code
Write Code as follows:
*(a). RadioButton.cs
Create a file named RadioButton.cs
Below are our imports. Included are the System.Windows.Forms
:
using System;
using System.Windows.Forms;
The main entry point for the application. Enable VisualStyles
and invoke Application.Run()
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
Define our Form1
Constructor
public Form1() {
Add several controls using the AddRange()
method. You pass it an array of controls, in this case Radiobuttons
Controls.AddRange(new Control[] { radioButton1, radioButton2, radioButton3, radioButton4, radioButton5, label1 });
Set text property of the RadioButton using the Text
property
Text = "Radio button example";
You can set the RadioButton to be checked by default using the AutoCheck
property
radioButton1.AutoCheck = false;
You can also set the Checked
property to true or false to either check it or uncheck it maybe conditionally
radioButton1.Checked = true;
To handle click events of a radiobutton assign it a delegate as follows:
radioButton1.Click += delegate {
// Uncomments next line to check / uncheck radio button 1 (auto_check is false...)
//radioButton1.Checked = !radioButton1.Checked;
};
You can also set the font of a RadioButton using the Font
property. Assign it a System.Drawing.Font
as follows:
radioButton1.Font = new System.Drawing.Font(radioButton1.Font, System.Drawing.FontStyle.Italic);
Because we are creating our RadioButton programmatically, rather than by using a designer, assign it a location using the Location
property and assign it a System.Drawing.Point
object:
radioButton1.Location = new System.Drawing.Point(30, 30);
You can also listen to radiobutton checked change using the CheckedChanged
property and by assigning it a delegate
as follows:
radioButton3.CheckedChanged += delegate (object sender, EventArgs e) {
label1.Text = string.Format("Radio 3 checked = {0}", radioButton3.Checked);
};
Define the following radiobuttons and a label as our instance fields:
private RadioButton radioButton1 = new RadioButton();
private RadioButton radioButton2 = new RadioButton();
private RadioButton radioButton3 = new RadioButton();
private RadioButton radioButton4 = new RadioButton();
private RadioButton radioButton5 = new RadioButton();
private Label label1 = new Label();
Here is the full code
using System;
using System.Windows.Forms;
namespace Examples {
class Form1 : Form {
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1() {
Controls.AddRange(new Control[] { radioButton1, radioButton2, radioButton3, radioButton4, radioButton5, label1 });
Text = "Radio button example";
radioButton1.AutoCheck = false;
radioButton1.Checked = true;
radioButton1.Click += delegate {
// Uncomments next line to check / uncheck radio button 1 (auto_check is false...)
//radioButton1.Checked = !radioButton1.Checked;
};
radioButton1.Font = new System.Drawing.Font(radioButton1.Font, System.Drawing.FontStyle.Italic);
radioButton1.Location = new System.Drawing.Point(30, 30);
radioButton1.Text = "radio 1";
radioButton2.Location = new System.Drawing.Point(30, 60);
radioButton2.Text = "radio 2";
radioButton3.CheckedChanged += delegate (object sender, EventArgs e) {
label1.Text = string.Format("Radio 3 checked = {0}", radioButton3.Checked);
};
radioButton3.Checked = true;
radioButton3.Location = new System.Drawing.Point(30, 90);
radioButton3.Text = "radio 3";
radioButton4.Appearance = Appearance.Button;
radioButton4.Location = new System.Drawing.Point(30, 120);
radioButton4.Text = "radio 4";
radioButton5.AutoCheck = false;
radioButton5.Appearance = Appearance.Button;
radioButton5.Checked = true;
radioButton5.Click += delegate {
// Uncomments next line to check / uncheck radio button 5 (auto_check is false...)
//radioButton5.Checked = !radioButton5.Checked;
};
radioButton5.Font = new System.Drawing.Font(radioButton1.Font, System.Drawing.FontStyle.Italic);
radioButton5.Location = new System.Drawing.Point(30, 150);
radioButton5.Text = "radio 5";
System.Diagnostics.Debug.WriteLine(string.Format("tb = {0}", radioButton3));
label1.AutoSize = true;
label1.Location = new System.Drawing.Point(30, 180);
}
private RadioButton radioButton1 = new RadioButton();
private RadioButton radioButton2 = new RadioButton();
private RadioButton radioButton3 = new RadioButton();
private RadioButton radioButton4 = new RadioButton();
private RadioButton radioButton5 = new RadioButton();
private Label label1 = new Label();
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
Example 2: GroupBoxAndRadioButton
This example will comprise the following files:
GroupBoxAndRadioButton.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project.
Step 2: Write Code
Write Code as follows:
*(a). GroupBoxAndRadioButton.cs
Create a file named GroupBoxAndRadioButton.cs
Here is the full code
using System;
using System.Windows.Forms;
namespace Examples {
class Form1 : Form {
// The main entry point for the application.
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1() {
this.StartPosition = FormStartPosition.Manual;
this.Location = new System.Drawing.Point(400, 200);
this.ClientSize = new System.Drawing.Size(300, 160);
this.Text = "GroupBox and RadioButton example";
this.groupBox1.Parent = this;
this.groupBox1.Text = "Group 1";
this.groupBox1.Bounds = new System.Drawing.Rectangle(10, 10, 135, 140);
this.radio1.Parent = this.groupBox1;
this.radio1.Location = new System.Drawing.Point(20, 20);
this.radio1.Text = "radio button 1";
this.radio1.Checked = true;
this.radio2.Parent = this.groupBox1;
this.radio2.Location = new System.Drawing.Point(20, 50);
this.radio2.Text = "radio button 2";
this.radio3.Parent = this.groupBox1;
this.radio3.Location = new System.Drawing.Point(20, 80);
this.radio3.Text = "radio button 3";
this.groupBox2.Parent = this;
this.groupBox2.Text = "Group 2";
this.groupBox2.Bounds = new System.Drawing.Rectangle(155, 10, 130, 140);
this.radio4.Parent = this.groupBox2;
this.radio4.Location = new System.Drawing.Point(20, 20);
this.radio4.Text = "radio button 4";
this.radio5.Parent = this.groupBox2;
this.radio5.Location = new System.Drawing.Point(20, 50);
this.radio5.Text = "radio button 5";
this.radio5.Checked = true;
this.radio6.Parent = this.groupBox2;
this.radio6.Location = new System.Drawing.Point(20, 80);
this.radio6.Text = "radio button 6";
}
private GroupBox groupBox1 = new GroupBox();
private GroupBox groupBox2 = new GroupBox();
private RadioButton radio1 = new RadioButton();
private RadioButton radio2 = new RadioButton();
private RadioButton radio3 = new RadioButton();
private RadioButton radio4 = new RadioButton();
private RadioButton radio5 = new RadioButton();
private RadioButton radio6 = new RadioButton();
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |