70-526 MCTS


GroupBox control

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.


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 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

  • GroupBox control


    << Previous Contents Next >>

    © Publicjoe, 2007