Introduction
The computer language C# (pronounced "See Sharp") is modern, object-oriented, and type-safe. C# allows programmers to create a wide range of secure and reliable programmes for use in.
A GroupBox is a frame display surrounding a set of controls and includes an optional title. A GroupBox can also be used to categorize the controls in a group. The GroupBox class represents the windows group box and has a variety of attributes, methods, and events. A group box is mainly used to organize a logical group of RadioButton controls.
There are two ways to create a GroupBox:
Design-Time
Run-Time
Design-Time
It's the most straightforward approach to making a GroupBox. We can create a GroupBox control using a Forms designer at design time.
To create a GroupBox control at design time, we follow some step which is shown below:
Step 1: As demonstrated in the figure below, create windows form with the help of the following steps:
Open Visual Studio → File → New → Project →WindowsFormApp → Project Name
Step 2: drag and drop a GroupBox onto a Form from the toolbox.
Step 3: After you drag and drop a GroupBox on a Form, the GroupBox looks like this.
Step 4: Add properties based on your requirement.
Output:
Run-Time
Using the syntax provided by the GroupBox class, you can build a GroupBox programmatically in this function. The steps below demonstrate how to dynamically set the create GroupBox:
Step 1: Creating a GroupBox.
GroupBox box = new GroupBox();
Step 2: Set the property of the GroupBox.
box.Location = new Point(180, 146);
box.Size = new Size(450, 150);
box.Text = "Gender";
box.Name = "Mybox";
Step 3: Add this GroupBox control to the form.
this.Controls.Add(box);
Step 4: Add other controls on the GroupBox.
box.Controls.Add(b1);
Example:
namespace WinFormsApp3
{
partial class Form1
{
/* <summary>
Required designer variable.
</summary> */
private System.ComponentModel.IContainer components = null;
/* <summary>
Clean up any resources being used.
</summary>
<param name="disposing">true if managed resources should be disposed;otherwise, false.</param> */
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/* <summary>
Required method for Designer support - do not modify the contents of this method with the code editor.
</summary> */
private void InitializeComponent()
{
// Creating and setting properties of the GroupBox
GroupBox box = new GroupBox();
{
box.Location = new Point(180, 146);
box.Size = new Size(450, 150);
box.Text = "Gender";
box.Name = "Mybox";
};
// Adding Groupbox in the form
this.Controls.Add(box);
// Creating and setting properties of the CheckBox
CheckBox b1 = new CheckBox();
{
b1.Location = new Point(40, 40);
b1.Size = new Size(100, 50);
b1.Text = "Male";
};
// Adding this control to the GroupBox
box.Controls.Add(b1);
// Creating and setting properties of the CheckBox
CheckBox b2 = new CheckBox();
{
b2.Location = new Point(160, 40);
b2.Size = new Size(100, 50);
b2.Text = "Female";
};
// Adding this control to the GroupBox
box.Controls.Add(b2);
// Creating and setting properties of the CheckBox
CheckBox b3 = new CheckBox();
{
b3.Location = new Point(320, 40);
b3.Size = new Size(100, 50);
b3.Text = "Others";
};
// Adding this control to the GroupBox
box.Controls.Add(b3);
}
#endregion
}
Output: