C# | GroupBox Class - GeeksforGeeks

05 Sep, 2019
In Windows form, GroupBox is a container which contains multiple controls on it and the controls are related to each other. Or in other words, GroupBox is a frame display around a group of controls with a suitable optional title. Or a GroupBox is used to categorize the related controls in a group. The GroupBox class is used to represent the windows group box and also provide different types of properties, methods, and events. It is defined under System.Windows.Forms namespace. The main use of a group box is to hold a logical group of RadioButton controls.
In C# you can create a GroupBox in the windows form by using two different ways:
1. Design-Time: It is the easiest way to create a GroupBox as shown in the following steps:
Step 1: Create a windows form as shown in the below image:Visual Studio -> File -> New -> Project -> WindowsFormApp
Step 2: Next, drag and drop the GroupBox from the toolbox on the form.
Step 3: After drag and drop you will go to the properties of the GroupBox to modify GroupBox according to your requirement.Output:
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can create a GroupBox programmatically with the help of syntax provided by the GroupBox class. The following steps show how to set the create GroupBox dynamically:
Step 1: Create a GroupBox using the GroupBox() constructor is provided by the GroupBox class.// Creating a GroupBox
GroupBox box = new GroupBox(); 

// Creating a GroupBox
GroupBox box = new GroupBox(); 

Step 2: After creating GroupBox, set the property of the GroupBox provided by the GroupBox class.// Setting the location of the GroupBox
box.Location = new Point(179, 145);

// Setting the size of the GroupBox
box.Size = new Size(329, 94);

// Setting text the GroupBox
box.Text = "Select Gender";

// Setting the name of the GroupBox
box.Name = "MyGroupbox";

// Setting the location of the GroupBox
box.Location = new Point(179, 145);

// Setting the size of the GroupBox
box.Size = new Size(329, 94);

// Setting text the GroupBox
box.Text = "Select Gender";

// Setting the name of the GroupBox
box.Name = "MyGroupbox";

Step 3: And last add this GroupBox control to the form and also add other controls on the GroupBox using the following statements:// Adding groupbox in the form
this.Controls.Add(box);

and 

// Adding this control to the GroupBox
box.Controls.Add(b2);
Example: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 WindowsFormsApp45 {  public partial class Form1 : Form {      public Form1()    {        InitializeComponent();    }      private void Form1_Load(object sender, EventArgs e)    {        // Creating and setting         // properties of the GroupBox        GroupBox box = new GroupBox();        box.Location = new Point(179, 145);        box.Size = new Size(329, 94);        box.Text = "Select Gender";        box.Name = "MyGroupbox";          // 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, 42);        b1.Size = new Size(49, 20);        b1.Text = "Male";          // Adding this control         // to the GroupBox        gbox.Controls.Add(b1);          // Creating and setting         // properties of the CheckBox        CheckBox b2 = new CheckBox();        b2.Location = new Point(183, 39);        b2.Size = new Size(69, 20);        b2.Text = "Female";          // Adding this control        // to the GroupBox        box.Controls.Add(b2);    }}}Output:
// Adding groupbox in the form
this.Controls.Add(box);

and 

// Adding this control to the GroupBox
box.Controls.Add(b2);

Example:
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 WindowsFormsApp45 {  public partial class Form1 : Form {      public Form1()    {        InitializeComponent();    }      private void Form1_Load(object sender, EventArgs e)    {        // Creating and setting         // properties of the GroupBox        GroupBox box = new GroupBox();        box.Location = new Point(179, 145);        box.Size = new Size(329, 94);        box.Text = "Select Gender";        box.Name = "MyGroupbox";          // 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, 42);        b1.Size = new Size(49, 20);        b1.Text = "Male";          // Adding this control         // to the GroupBox        gbox.Controls.Add(b1);          // Creating and setting         // properties of the CheckBox        CheckBox b2 = new CheckBox();        b2.Location = new Point(183, 39);        b2.Size = new Size(69, 20);        b2.Text = "Female";          // Adding this control        // to the GroupBox        box.Controls.Add(b2);    }}}
Output:
CSharp-Windows-Forms-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# Dictionary with examples
C# | Method Overriding
C# | Constructors
C# | Class and Object
Introduction to .NET Framework
Extension Method in C#
Difference between Ref and Out keywords in C#
C# | String.IndexOf( ) Method | Set - 1
C# | Delegates
C# | Data Types