How to set the Name 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. In GroupBox, you can set the name of the GroupBox in the form using Name Property. You can set this property in two different ways:
1. Design-Time: It is the easiest way to set the name of the 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 to the form as shown in the below image:

- Step 3: After drag and drop you will go to the properties of the GroupBox and set the name of the GroupBox as shown in the below image;

Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can set the name of the GroupBox programmatically with the help of given syntax:
public string Name { get; set; }
The value of this property is of System.String type and this string represent the name of the GroupBox. The following steps show how to set the name of the GroupBox dynamically:
- Step 1: Create a GroupBox using the GroupBox() constructor is provided by the GroupBox class.
// Creating a GroupBox GroupBox gbox = new GroupBox();
- Step 2: After creating GroupBox, set the Name property of the GroupBox provided by the GroupBox class.
// Setting the name gbox.Name = "Mybox";
- 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(gbox); and // Adding this control to the GroupBox gbox.Controls.Add(c2);
Example:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApp45 {publicpartialclassForm1 : Form {publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender, EventArgs e){// Creating and setting properties// of the GroupBoxGroupBox gbox =newGroupBox();gbox.Location =newPoint(179, 145);gbox.Size =newSize(329, 94);gbox.Text ="Select Gender";gbox.Name ="Mybox";// Adding groupbox in the formthis.Controls.Add(gbox);// Creating and setting// properties of the CheckBoxCheckBox c1 =newCheckBox();c1.Location =newPoint(40, 42);c1.Size =newSize(49, 20);c1.Text ="Male";// Adding this control// to the GroupBoxgbox.Controls.Add(c1);// Creating and setting properties// of the CheckBoxCheckBox c2 =newCheckBox();c2.Location =newPoint(183, 39);c2.Size =newSize(69, 20);c2.Text ="Female";// Adding this control// to the GroupBoxgbox.Controls.Add(c2);}}}chevron_rightfilter_noneOutput:

Recommended Posts:
- How to Add Text in the GroupBox in C#?
- C# | GroupBox Class
- How to Set the Visibility of the GroupBox in C#?
- How to set the Location of the GroupBox in C#?
- How to set the Size of the GroupBox in C#?
- How to set Foreground Color of the GroupBox in C#?
- How to set the Background Color of the GroupBox in C#?
- How to set the Auto Size Mode of the GroupBox in C#?
- How to set the Font of Content present in the GroupBox in C#?
- Hello World Program : First program while learning Programming
- Range and Indices in C# 8.0
- Range Structure in C# 8.0
- Basics Operations of File and Directory in C#
- Check if the given ranges are equal or not in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


