GroupBox

A GroupBox is a frame with a caption drawn in its border. It exists for two reasons: it labels a set of related controls, and it is a container, which is what makes two independent groups of radio buttons possible on one form. The second reason is the technical one, and the first is why a form is readable.

A frame with a caption

using System; using System.Drawing; using System.Windows.Forms; internal static class Program { [STAThread] private static void Main() { ApplicationConfiguration.Initialize(); Application.Run(new Form1()); } } internal sealed class Form1 : Form { private readonly RadioButton _small = new() { Text = "Small", Location = new Point(16, 24), AutoSize = true }; private readonly RadioButton _large = new() { Text = "Large", Location = new Point(16, 52), AutoSize = true }; private readonly RadioButton _pickup = new() { Text = "Pick up", Location = new Point(16, 24), AutoSize = true }; private readonly RadioButton _delivery = new() { Text = "Deliver", Location = new Point(16, 52), AutoSize = true }; public Form1() { Text = "An order"; ClientSize = new Size(420, 220); var size = new GroupBox { Text = "Size", Location = new Point(16, 16), Size = new Size(180, 90), }; size.Controls.Add(_small); size.Controls.Add(_large); var shipping = new GroupBox { Text = "Shipping", Location = new Point(212, 16), Size = new Size(180, 90), }; shipping.Controls.Add(_pickup); shipping.Controls.Add(_delivery); _small.Checked = true; _pickup.Checked = true; Controls.Add(size); Controls.Add(shipping); } }

dotnet build app.csproj

Build succeeded.
0 Warning(s)
0 Error(s)

The listing is built and not run. Two groups, two independent choices: picking Large does not clear Pick up, because the two pairs live in different containers. Without the group boxes, all four buttons would be one group of one-of-four, which is the bug this control prevents.

What is inside

The children of a group box are in its Controls collection, and their Location is relative to the group box, not to the form. _small at (16, 24) sits 16 pixels from the left edge of its frame and 24 from the top, which leaves room for the caption. The relation is the useful part: move the group box and its contents move with it, hide it and its contents disappear.

The same holds for every container: a Panel, a TabPage, a SplitContainer panel. Coordinates are always relative to the parent, and Parent gives the parent back. That is why a control cannot be on two forms at once: adding it to a second container removes it from the first.

using System; using System.Drawing; using System.Windows.Forms; internal sealed class Form1 : Form { public Form1() { Text = "Walking the tree"; ClientSize = new Size(360, 180); var group = new GroupBox { Text = "Contact", Location = new Point(16, 16), Size = new Size(300, 130), }; group.Controls.Add(new TextBox { Location = new Point(16, 28), Width = 260 }); group.Controls.Add(new TextBox { Location = new Point(16, 64), Width = 260 }); group.Controls.Add(new CheckBox { Text = "Primary", Location = new Point(16, 96), AutoSize = true }); Controls.Add(group); foreach (Control child in group.Controls) { child.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; } } }

That block is a fragment: a form with no Main, so the checker counts it and does not build it. The loop sets the anchors of every child in three lines, which is how a form with a dozen fields stays aligned when the window is widened. Controls is an ordinary collection, and it can be walked, filtered with OfType<TextBox>() and searched by name with Controls["textBox1"].

Enabling a whole group

Enabled and Visible apply to a container and everything inside it. A group box set to Enabled = false greys out its caption and every child, and none of them can take the focus. That is the cheapest way to express "this part of the form does not apply yet":

using System; using System.Drawing; using System.Windows.Forms; internal static class Program { [STAThread] private static void Main() { ApplicationConfiguration.Initialize(); Application.Run(new Form1()); } } internal sealed class Form1 : Form { private readonly GroupBox _advanced = new() { Text = "Advanced options", Location = new Point(16, 64), Size = new Size(320, 120), Enabled = false, }; public Form1() { Text = "Advanced"; ClientSize = new Size(360, 200); var show = new CheckBox { Text = "Show the advanced options", Location = new Point(16, 24), AutoSize = true, }; var retries = new NumericUpDown { Location = new Point(16, 32), Maximum = 10, Value = 3, Width = 80, }; var keepLog = new CheckBox { Text = "Keep a log", Location = new Point(16, 72), AutoSize = true }; _advanced.Controls.Add(retries); _advanced.Controls.Add(keepLog); show.CheckedChanged += (sender, e) => _advanced.Enabled = show.Checked; Controls.Add(show); Controls.Add(_advanced); } }

One line, _advanced.Enabled = show.Checked, enables the whole group. Setting Visible = false instead would also work and would remove the space the group occupies, which changes the layout; disabling keeps the form stable and tells the user that the options exist.

GroupBox or Panel

Exercises

  1. Move _large from the size group to the form and explain which group it now belongs to.
  2. Disable the shipping group and describe what a user sees and can do.
  3. Replace the group box with a panel and keep the radio grouping, then say what the form loses.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What makes two groups of radio buttons independent on one form?

    Answer

    Each group is in a different container, such as a GroupBox

    Radio buttons group by parent control; a container is the boundary.

  2. Relative to what is a control's Location inside a GroupBox?

    Answer

    The GroupBox's client area

    Coordinates are relative to the parent, which is the group box here.

  3. What does adding a control to a second container do?

    Answer

    Removes it from the first, because a control has one parent

    Parent is single; Controls.Add reparents the control.

  4. What happens to the children when a GroupBox is disabled?

    Answer

    They are disabled too and take no focus

    Enabled flows down the container tree.

  5. Which property of a GroupBox holds its children?

    Answer

    Controls

    Every container in WinForms exposes its children through Controls.

  6. What is the difference between GroupBox and Panel?

    Answer

    The group box has a visible frame and a caption; the panel can scroll and border optionally

    Both are containers; the frame and the caption are the group box's addition.

  7. Which loop sets an anchor on every child of a group?

    Answer

    foreach (Control child in group.Controls)

    Controls is an enumerable collection of Control.

  8. How do you find a child control by its Name?

    Answer

    Controls["textBox1"]

    The indexer of the collection looks up the Name property.

  9. Which is better for a group that must disappear and leave no gap?

    Answer

    Visible = false

    A hidden control takes no space; a disabled one keeps its place and shows the options exist.

  10. Why does the group box caption matter for readability?

    Answer

    It labels the set of related controls, so a form reads as sections

    Grouping works through the container; the caption is for the reader.

Every listing on this page was built with the .NET SDK 10.0.303 for net10.0-windows against the targeting pack Microsoft.WindowsDesktop.App.Ref 10.0.15 from the offline feed, in a Docker container on Alpine Linux 3.24.3 (x86_64). The programs were built, not run on Linux: WinForms needs Windows.

Article author: Arthur Isaev