Table of contents
1.
Introduction
1.1.
There are two ways to create a GroupBox:
1.2.
Design-Time
1.3.
Run-Time
2.
Design-Time
3.
Run-Time 
4.
Constructors:
5.
Properties
6.
Frequently Asked Questions
6.1.
What is the use of the group box in C#?
6.2.
What is an advantage of using a group box?
6.3.
How can we add the control into a group box?
6.4.
What is a group box control?
6.5.
What is CheckBox C#?
7.
Conclusion
Last Updated: Mar 27, 2024

C# | GroupBox Class

Author Aditya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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
    }
You can also try this code with Online C++ Compiler
Run Code

 

Output:

     

Constructors:

  • GroupBox(): This constructor is used to create a new GroupBox instance.

Properties

  • Location: Gets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container in C# GroupBox Location.
  • TabStop: This property is used to acquire or set a value that indicates if the user can focus on the GroupBox by using the TAB key.
  • Visible: This property gets or sets a value that indicates whether the control and all of its child controls are visible.
  • Size: The height and width of the control are obtained or set using this property.
  • AutoSize: This property allows you to retrieve or set a value that indicates whether the control resizes in response to its contents.
  • AutoSizeMode: When the GroupBox's AutoSize property is enabled, this property controls how the GroupBox acts.
  • BackColor: The background color of the control is retrieved or set using this attribute.

Also see, Ienumerable vs Iqueryable

Frequently Asked Questions

What is the use of the group box in C#?

In a Windows form, a GroupBox is a container that holds many controls that are related to one another. Or, to put it another way, GroupBox is a frame display that surrounds a group of controls and includes an optional title. A GroupBox can also be used to categorize the controls in a group.

What is an advantage of using a group box?

A Group Box consists of a graphical outline of the container's size and a caption at the top. A panel may automatically scroll its contents, whereas a group box cannot do so without some sort of intervention (Panel inside a group box).

How can we add the control into a group box?

Add other controls to the group box by drawing them inside it. If you wish to enclose existing controls in a group box, select all the controls, copy them to the Clipboard, select the GroupBox control, and paste them into the group box. Drag them into the group box as well.

What is a group box control?

It is a container control that holds a collection of Windows Forms child controls. The objective of GroupBox is to provide user interfaces that group together relevant controls. A GroupBox control is a container control that holds a collection of Windows Forms child controls.

What is CheckBox C#?

The CheckBox control is a component of a Windows form that collects user input. In other words, the CheckBox control allows us to select one or more things from a list, as well as providing options such as yes or no, true or false, and so on. It can be shown as a picture, text, or both.

Conclusion

In this article, we discussed GroupBox Class in C#. We also discussed different ways to create a GroupBox. And finally, in the end, we have shown an example of how to create a radio button in GroupBox.

We hope that our blog has helped you enhance your knowledge regarding GroupBox Class in C#. Please check out these articles to improve your understanding of C#: Static Class in C#Operators in C#Properties of C#, singleton design pattern in c#

 

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Please upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass