Let us look at a simple C# Windows Forms GroupBoxAndRadioButton example.
Example 1: GroupBox with RadioButton
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 |
Example 2: GroupBoxAndCheckBox
This example will comprise the following files:
GroupBoxAndCheckBox.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). GroupBoxAndCheckBox.cs
Create a file named GroupBoxAndCheckBox.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());
}
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 CheckBox example";
this.group1.Parent = this;
this.group1.Text = "Group 1";
this.group1.Bounds = new System.Drawing.Rectangle(10, 10, 135, 140);
this.check1.Parent = this.group1;
this.check1.Location = new System.Drawing.Point(20, 20);
this.check1.Text = "check 1";
this.check2.Parent = this.group1;
this.check2.Location = new System.Drawing.Point(20, 50);
this.check2.Text = "check 2";
this.check2.Checked = true;
this.check3.Parent = this.group1;
this.check3.Location = new System.Drawing.Point(20, 80);
this.check3.Text = "check 3";
this.group2.Parent = this;
this.group2.Text = "Group 2";
this.group2.Bounds = new System.Drawing.Rectangle(155, 10, 135, 140);
this.check4.Parent = this.group2;
this.check4.Location = new System.Drawing.Point(20, 20);
this.check4.Text = "check 4";
this.check5.Parent = this.group2;
this.check5.Location = new System.Drawing.Point(20, 50);
this.check5.Text = "check 5";
this.check6.Parent = this.group2;
this.check6.Location = new System.Drawing.Point(20, 80);
this.check6.Text = "check 6";
this.check6.Checked = true;
}
private GroupBox group1 = new GroupBox();
private GroupBox group2 = new GroupBox();
private CheckBox check1 = new CheckBox();
private CheckBox check2 = new CheckBox();
private CheckBox check3 = new CheckBox();
private CheckBox check4 = new CheckBox();
private CheckBox check5 = new CheckBox();
private CheckBox check6 = new CheckBox();
}
}
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 |