How to set the Auto Size Mode of the GroupBox in C#?
Last Updated :
02 Aug, 2019
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 a value which indicates how GroupBox behaves when the value of the AutoSize Property is set to true, using
AutoSizeMode Property. The value of this property is defined under AutoSizeMode enum and the values are:
- GrowOnly: This value indicates that the GroupVox grow according to the content, but does not shrink if the content is less.
- GrowAndShrink: This value indicates the GroupBox grows and shrink according to the content present in it.
The default value of this property is GrowOnly. You can set this property in two different ways:
1. Design-Time: It is the easiest way to set the AutoSizeMode property 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 control from the toolbox on 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 AutoSizeMode property 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 how GroupBox behave when the AutoSize property set to be true, programmatically with the help of given syntax:
public System.Windows.Forms.AutoSizeMode AutoSizeMode { get; set; }
Here, AutoSizeMode is used to set the value of this property. The following steps show how to set the AutoSizeMode property 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 AutoSizeMode property of the GroupBox provided by the GroupBox class.
// Setting Auto Size Mode
gbox.AutoSizeMode = AutoSizeMode.GrowAndShrink;
- 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:
CSharp
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 WindowsFormsApp46 {
public partial class Form1 : Form {
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Creating and setting
// properties of the GroupBox
GroupBox gbox = new GroupBox();
gbox.Location = new Point(179, 145);
gbox.Text = "Select Gender";
gbox.Name = "Mybox";
gbox.Font = new Font("Colonna MT", 12);
gbox.Visible = true;
gbox.AutoSize = true;
gbox.AutoSizeMode = AutoSizeMode.GrowAndShrink;
// Adding groupbox in the form
this.Controls.Add(gbox);
// Creating and setting
// properties of the CheckBox
CheckBox c1 = new CheckBox();
c1.Location = new Point(40, 42);
c1.Size = new Size(69, 20);
c1.Text = "Male";
// Adding this control
// to the GroupBox
gbox.Controls.Add(c1);
// Creating and setting
// properties of the CheckBox
CheckBox c2 = new CheckBox();
c2.Location = new Point(183, 39);
c2.Size = new Size(79, 20);
c2.Text = "Female";
// Adding this control
// to the GroupBox
gbox.Controls.Add(c2);
}
}
}
Output:

Similar Reads
How to set the Size of the GroupBox in C#? In Windows form, 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. I
3 min read
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.
3 min read
How to set the Auto Size Mode of FlowLayoutPanel in C#? In Windows Forms, FlowLayoutPanel control is used to arrange its child controls in a horizontal or vertical flow direction. Or in other words, FlowLayoutPanel is a container which is used to organize different or same types of controls in it either horizontally or vertically. In FlowLayoutPanel cont
4 min read
How to set the Size of the ComboBox in C#? In Windows forms, ComboBox provides two different features in a single control, it means ComboBox works as both TextBox and ListBox. In ComboBox, only one item is displayed at a time and the rest of the items are present in the drop-down menu. You are allowed to set the size to your ComboBox by usin
3 min read
How to set the size of the Button in C#? A Button is an essential part of an application, or software, or webpage. It allows the user to interact with the application or software. In windows forms, you are allowed to set the size of the button automatically using the AutoSize Property of the button. It is provided by Button class which hel
3 min read
How to set the Location 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.
3 min read