Skip to content
Related Articles

Related Articles

C# | GroupBox Class
  • Last Updated : 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:

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(); 
    
  • 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);
    
    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:

Constructor

ConstructorDescription
GroupBox()This Constructors is used to initializes a new instance of the GroupBox class.

Properties

PropertyDescription
AutoSizeThis property is used to get or set a value that indicates whether the control resizes based on its contents.
AutoSizeModeThis property indicates how the GroupBox behaves when its AutoSize property is enabled.
BackColorThis property is used to get or set the background color for the control.
BorderStyleThis property indicates the border style for the control.
DisplayRectangleThis property is used to get a rectangle that represents the dimensions of the GroupBox.
FontThis property is used to get or set the font of the text displayed by the control.
ForeColorThis property is used to get or set the foreground color of the control.
HeightThis property is used to get or set the height of the control.
LocationThis 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.
NameThis property is used to get or set the name of the control.
TabStopThis 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.
SizeThis property is used to get or set the height and width of the control.
VisibleThis property is used to get or set a value indicating whether the control and all its child controls are displayed.
WidthThis property is used to get or set the width of the control.



My Personal Notes arrow_drop_up
Recommended Articles
Page :