In Windows Forms, GroupBox is a container that contains multiple controls, and the controls are related to each other. In other words, GroupBox is a frame display around a group of controls with a suitable optional title. A GroupBox in C# is used to categorize the related controls in a group. The GroupBox class is used to represent the Windows group box and also provides 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 the RadioButton controls.
Why Use GroupBox?
GroupBox is used for the following reasons, which are listed below:
- It groups related controls, both visually and logically.
- It makes the form look organized and easier to use.
- It helps to manage several controls as one unit.
- It adds a clear title around the group for better understanding.
There are mainly two ways to create a GroupBox in Windows forms which are mentioned below.
- Drag and drop ( Design-Time)
- Custom GroupBox( Run-Time)
1. Drag and drop ( Design-Time)
This is the easiest way to create a GroupBox in Windows Forms using Visual Studio we just have to open the toolbox and drag and drop the button on the form in the designer and further we can change the appearance of the GroupBox using the properties. Follow these steps to create a GroupBox.
Step 1: Now locate the project with the name here we are using the default name which is Form1 and it will open a form in the editor that we can further modify.

In the image, we have two files that are open one Design and there is Form1.cs these two play a major role. We use the Form 1.cs file for the custom logic.
Step 2: Now open a Toolbox go to the view > Toolbox or ctrl + alt + x.

Step 3. Now open the common controls and drag and drop the GroupBox on the form where we want it to be placed.
Step 4. Now open the properties of the button right-click on the GroupBox and it will open the properties solution explorer now we can change the appearance and the behaviour of the GroupBox.
Output:

2. Custom GroupBox (Run Time)
In this method, we are going to modify the Form1.cs file and add custom code modification in C# with the help of the GroupBox class. The following steps show how to create a GroupBox dynamically:
Step 1: Create a GroupBox using the GroupBox() constructor is provided by the GroupBox class.
// 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";
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);
// Adding this control to the GroupBox
box.Controls.Add(b2);
Step 4: Now double-click on the form in Design and it will open the Form1.cs file where code is written in C#. Here, the program file is Form 1.cs. Now write this code in Form1.cs file.
Form1.cs file:
C#
namespace WinFormsApp1
{
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
box.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:

Let's consider a user registration form, where users choose their gender. We are going to put options like male, female and other inside a GroupBox called Gender. It also helps users move through the form easily using the keyboard.
Example:
C#
GroupBox genderGroup = new GroupBox
{
Text = "Gender",
Location = new Point(20, 20),
Size = new Size(200, 100)
};
RadioButton maleRadio = new RadioButton { Text = "Male", Location = new Point(10, 20) };
RadioButton femaleRadio = new RadioButton { Text = "Female", Location = new Point(10, 45) };
RadioButton otherRadio = new RadioButton { Text = "Other", Location = new Point(10, 70) };
genderGroup.Controls.Add(maleRadio);
genderGroup.Controls.Add(femaleRadio);
genderGroup.Controls.Add(otherRadio);
this.Controls.Add(genderGroup);
Now, lets understand which one is better to use, for this we are going to see the difference between GroupBox and Panel.
GroupBox vs. Panel
The table below demonstrates the difference between GroupBox and Panel.
Feature | GroupBox | Panel |
---|
Border | It has a visible border with title | No border by default |
---|
Title | Supports a visible title/text | Does not support titles |
---|
Use Case | Group related controls visually | Used for layout or scrollable areas |
---|
Performance | Slightly heavier due to drawing border and text | Faster and more flexible |
---|
Note: We can use GroupBox when we want to visually separate and title a logical group of related controls and use Panel when we need a container mainly for layout purposes without a visible border or title.
Constructor
GroupBox(): This constructor is used to initialise a new instance of the GroupBox class.
Properties
Properties | Description |
---|
AutoSize | This property is used to get or set a value that indicates whether the control resizes based on its contents. |
AutoSizeMode | This property indicates how the GroupBox behaves when its AutoSize property is enabled. |
BackColor | This property is used to get or set the background ccolourfor the control. |
BorderStyle | This property indicates the border style for the control. |
DisplayRectangle | This property is used to get a rectangle that represents the dimensions of the GroupBox. |
Font | This property is used to get or set the font of the text displayed by the control. |
ForeColor | This property is used to get or set the foreground colour of the control. |
Height | This property is used to get or set the height of the control. |
Location | This property is used to get or set the coordinates of the upper-left corner of the GroupBox control relative to the upper-left corner of its form. |
Name | This property is used to get or set the name of the control. |
TabStop | This property is used to get or set a value that shows whether the user can press the TAB key to provide the focus to the GroupBox. |
Size | This property is used to get or set the height and width of the control. |
Visible | This property is used to get or set a value indicating whether the control and all its child controls are displayed. |
Width | This property is used to get or set the width of the control.
|
Common Issues Faced When using GroupBox
Some of the common issues faced when using GroupBox is listed below:
- Controls Not Visible Inside GroupBox: Sometimes controls does not show up in a GroupBox because their position is set based on the whole form.
- Tab Order Confusion: Tab navigation can get confusing if the indexes are not set right.
- Event Handling Challenges: Events inside controls don not reach the GroupBox.
- Performance Issues: Adding lots of controller can cause flicker.
- Too Many Nested GroupBoxes: Too much nesting can confuse users.
Similar Reads
C# | GroupBox Class
In Windows Forms, GroupBox is a container that contains multiple controls, and the controls are related to each other. In other words, GroupBox is a frame display around a group of controls with a suitable optional title. A GroupBox in C# is used to categorize the related controls in a group. The Gr
7 min read
C# | ListBox Class
In Windows Forms, the ListBox control is used to show multiple elements in a list, from which a user can select one or more elements, and the elements are generally displayed in multiple columns. The ListBox class in C# is used to represent the Windows list box and also provides different types of p
7 min read
C# | Collection Class
.math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-chil
5 min read
C# Class and Objects
Class and Object are the basic concepts of Object-Oriented Programming which revolve around real-life entities. A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member functions which define actions) into a single uni
5 min read
C# Stack Class
In C#, the Stack<T> class represents a Last-in-First-out (LIFO) collection of objects. The stack is the part of the System.Collections.Generic namespace. This class allows us to push elements onto the stack, pop elements from the stack, and peek at the top element without removing it.The capac
5 min read
C# TextBox Controls
In Windows forms, TextBox plays an important role. With the help of TextBox, the user can enter data in the application, it can be of a single line or multiple lines. The TextBox is a class and it is defined under System.Windows.Forms namespace.Ways to Create a TextBox In Windows FormsThere are main
5 min read
How to Add Text in the GroupBox in C#?
In Windows Forms, GroupBox is a container which contains multiple controls in 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.
3 min read
How to set Foreground Color of the GroupBox in C#?
In Windows Forms, GroupBox is a container which contains multiple controls in 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.
3 min read
C# Tuple<T1,T2,T3,T4,T5,T6,T7> Class
Tuple<T1, T2, T3, T4, T5, T6, T7> class creates a 7-tuple or septuple. It represents a tuple that contains seven elements. We can instantiate a Tuple<T1, T2, T3, T4, T5, T6, T7> object by calling either the Tuple<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) constructor o
3 min read
How to set the Background Color of the GroupBox in C#?
In Windows form, GroupBox is a container which contains multiple controls in 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. I
3 min read