GroupBox

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Exam Prep. Guides
70-505 Study Guide
70-526 Study Guide

1. Forms Controls

2. Integrating Data
3. Printing/Reporting
4. Enhancing Usability
5. Asynchronous Prog.
6. Forms Controls
7. Configure/Deploy

edit
Exam 70-526 Preparation Guide: Group and arrange controls by using the GroupBox control.


Contents


The GroupBox control displays a frame around a group of controls with or without a caption. GroupBoxes are used to subdivide a form by function, giving the user a logical visual cue of control grouping

You can add controls to the GroupBox at design time and runtime. When you move the GroupBox control, all of its contained controls move too.

When you create a new group box, it has a border by default, and its caption is set to the name of the control

Image:526-34.jpg


Inheritance hierarchy

System.Object 
  System.MarshalByRefObject 
    System.ComponentModel.Component 
      System.Windows.Forms.Control 
        System.Windows.Forms.GroupBox


Useful properties

  • BackColor - The background colour of the panel is defaulted to System.Drawing.SystemColors.Control, but this can be set to any colour you like
  • BackgroundImage - Instead of a single colour, an image can be displayed as the background.
  • Controls - Gets the collection of controls contained within the control.
  • Enabled - if this property is set to false, the controls contained within the GroupBox will also be disabled.
  • FlatStyle - Gets or sets the flat style appearance of the group box control.
  • TabIndex - Gets or sets the tab order of the control within its container. (inherited from Control).
  • TabStop - Gets or sets a value indicating whether the user can give the focus to this control using the TAB key.
  • Text - Caption text.
Note:
The GroupBox cannot display a scroll bar. If you need a control similar to a GroupBox that can contain a scroll bar, see the Panel control.


Add a control to a GroupBox manually

The GroupBox control MSDN page has the following example showing how to add a GroupBox and some controls within it.

private void InitializeMyGroupBox()
{
  // Create and initialize a GroupBox and a Button control.
  GroupBox groupBox1 = new GroupBox();
  Button button1 = new Button();
  button1.Location = new Point(20,10);
     
  // Set the FlatStyle of the GroupBox.
  groupBox1.FlatStyle = FlatStyle.Flat;
 
  // Add the Button to the GroupBox.
  groupBox1.Controls.Add(button1);
 
  // Add the GroupBox to the Form.
  Controls.Add(groupBox1);
 
  // Create and initialize a GroupBox and a Button control.
  GroupBox groupBox2 = new GroupBox();
  Button button2 = new Button();
  button2.Location = new Point(20, 10);
  groupBox2.Location = new Point(0, 120);
  
  // Set the FlatStyle of the GroupBox.
  groupBox2.FlatStyle = FlatStyle.Standard;
 
  // Add the Button to the GroupBox.
  groupBox2.Controls.Add(button2);
 
  // Add the GroupBox to the Form.
  Controls.Add(groupBox2);
}


MSDN references


Previous_Page_.gif Next_Page_.gif
© 2007-2008 Mike Kitchen

Personal tools