How to use Groupbox control in C#

A group box is a labeled rectangular frame that surrounds a set of related controls. A group box is a way to show relationships visually; aside from possibly providing an access key for a group of controls, it provides no functionality [from the MSDN]. Or we can say GroupBox control is a container control that is used to place Windows Forms child controls in a group.

The purpose of a GroupBox is to define user interfaces where we can categories related controls in a group.

We can create group box on windows form in design time and run time also.

Groupbox control in CSharp

Add groupbox on windows form:

if you want to add group box control on the windows form in design time then we can drag and drop GroupBox control on windows from from toolbox and then we can set its properties like width,height and size etc from Properties window.

if you want to create group box at run time then following example show that:

 private void AddGroupBox()
        {
            GroupBox GroupBox1 =new GroupBox();
            GroupBox1.Name = "GroupBox1";
            GroupBox1.Location = new Point(20, 20);
            GroupBox1.Size=new Size(300,100);
            GroupBox1.BackColor = Color.gray;
            GroupBox1.Text = "Student information";
            GroupBox1.Font = new Font("Calibri", 12);
            GroupBox1.FlatStyle = FlatStyle.Standard;
 
            this.Controls.Add(GroupBox1);
        }

Note:
1. We can add controls to the Group Box by using the Add method of the Controls property, like this

 Button button1 = new Button();
 button1.Location = new Point(30,30);
 button1.Text="Ok";
 GroupBox1.Controls.Add(button1);

2. The GroupBox cannot display a scroll bar. If you need a control similar to a GroupBox that can contain a scroll bar.
3. Important use for a group box is to contain a logical group of RadioButton controls. If you have two group boxes,each of which contain several option buttons (also known as radio buttons), each group of buttons is mutually exclusive, setting one option value per group.