C# | GroupBox Class

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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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);
        }
    }
    }

    chevron_right

    
    

    Output:

Constructor

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

Properties

Property 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 color for 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 color 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.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.