Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | ListBox Class
Next article icon

C# | GroupBox Class

Last Updated : 05 Sep, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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.


Next Article
C# | ListBox Class
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp-Windows-Forms-Namespace

Similar Reads

  • C# | ListBox Class
    In Windows Forms, ListBox control is used to show multiple elements in a list, from which a user can select one or more elements and the elements are generally displayed in multiple columns. The ListBox class is used to represent the windows list box and also provide different types of properties, m
    5 min read
  • C# | Collection Class
    .math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-chil
    5 min read
  • C# Class and Objects
    Class and Object are the basic concepts of Object-Oriented Programming which revolve around real-life entities. A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member functions which define actions) into a single uni
    5 min read
  • C# Stack Class
    In C#, the Stack<T> class represents a Last-in-First-out (LIFO) collection of objects. The stack is the part of the System.Collections.Generic namespace. This class allows us to push elements onto the stack, pop elements from the stack, and peek at the top element without removing it. The capa
    5 min read
  • C# TextBox Controls
    In Windows forms, TextBox plays an important role. With the help of TextBox, the user can enter data in the application, it can be of a single line or multiple lines. The TextBox is a class and it is defined under System.Windows.Forms namespace. Ways to Create a TextBox In Windows FormsThere are mai
    5 min read
  • ComboBox in C#
    In Windows Forms, the ComboBox control combines the features of a TextBox and a ListBox. It displays one item at a time, with additional items accessible through a drop-down menu. The ComboBox class is part of the System.Windows.Forms namespace. Ways to Create a ComboBox In Windows FormsThere are ma
    5 min read
  • How to Add Text in 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
  • C# Tuple<T1,T2,T3,T4,T5,T6> Class
    Tuple<T1, T2, T3, T4, T5, T6> class creates a 6-tuple or sextuple. It represents a tuple that contains six elements. You can instantiate a Tuple<T1, T2, T3, T4, T5, T6> object by calling either the Tuple<T1, T2, T3, T4, T5, T6>(T1, T2, T3, T4, T5, T6) constructor or by the static T
    3 min read
  • How to set Foreground Color 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
  • C# Tuple<T1,T2,T3,T4,T5,T6,T7> Class
    Tuple<T1, T2, T3, T4, T5, T6, T7> class creates a 7-tuple or septuple. It represents a tuple that contains seven elements. We can instantiate a Tuple<T1, T2, T3, T4, T5, T6, T7> object by calling either the Tuple<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) constructor o
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences