Make a checked GroupBox in C#

checked GroupBox

One useful control that’s missing from Visual Studio’s toolbox is a checked GroupBox. The control displays a CheckBox in its caption. When the user unchecks the CheckBox, all of the controls in the group are disabled.

While there is no such control, you can make a CheckBox and a GroupBox work together to provide a similar effect.

At design time, set the GroupBox‘s Text property to an empty string. Position a CheckBox inside the GroupBox so it looks like it is the GroupBox‘s caption. The result looks pretty good if you set the CheckBox‘s Location property to 6, 0.

When the user unchecks the CheckBox, you can disable the GroupBox. Unfortunately, that also disables the CheckBox so the user cannot re-check it.

You could place the CheckBox on top of the GroupBox rather than inside it, but the Form Designer tends to drop the CheckBox inside the GroupBox unless you’re very careful.

This example uses the following ManageCheckGroupBox method to handle these problems relatively easily.

private void ManageCheckGroupBox(CheckBox chk, GroupBox grp)
{
    // Make sure the CheckBox isn't in the GroupBox.
    // This will only happen the first time.
    if (chk.Parent == grp)
    {
        // Reparent the CheckBox so it's not in the GroupBox.
        grp.Parent.Controls.Add(chk);

        // Adjust the CheckBox's location.
        chk.Location = new Point(
            chk.Left + grp.Left,
            chk.Top + grp.Top);

        // Move the CheckBox to the top of the stacking order.
        chk.BringToFront();
    }

    // Enable or disable the GroupBox.
    grp.Enabled = chk.Checked;
}

This method checks whether the CheckBox is inside the GroupBox and, if it is, the method moves it into the GroupBox‘s parent. It then adjusts the CheckBox‘s location so it has the same position on the form. It also moves it to the front of the stacking order so it doesn’t end up behind the GroupBox.

Finally, depending on the CheckBox‘s state, the method enables or disables the GroupBox (which enables or disables all of the controls it contains).

The following code shows how the program responds when the user checks or unchecks the CheckBoxes.

private void chkBreakfast_CheckedChanged(object sender, EventArgs e)
{
    ManageCheckGroupBox(chkBreakfast, grpBreakfast);
}

private void chkLunch_CheckedChanged(object sender, EventArgs e)
{
    ManageCheckGroupBox(chkLunch, grpLunch);
}

These event handlers simply call the ManageCheckGroupBox method, passing it the appropriate CheckBox and its corresponding GroupBox.


Download Example   Follow me on Twitter   RSS feed


This entry was posted in controls, user interface and tagged , , , , , , , , , , . Bookmark the permalink.

6 Responses to Make a checked GroupBox in C#

  1. slearl says:

    I’m running the Zune theme with XP and the children of the groupbox don’t maintain the correct font color when the main checkbox is unchecked.

  2. Rod Stephens says:

    It sounds like the theme has its own idea of what colors a disabled radio button should have.

    You can either live with the theme’s choices or you can add to the code that disables the group box and make it change their colors to what you want. You can loop through the group box’s child controls to find them easily.

  3. Simon says:

    Simple and elegant, thank you.

  4. Sávio Dantas says:

    Great!!!

  5. Geoff says:

    When I open your files, it works when I duplicate what you have in my own it doesn’t work. I don’t see any difference. Very frustrating.

    • RodStephens says:

      Are you copying all of the code in the example? The post itself usually only includes the key pieces and the downloadable version often contains things that aren’t shown on the web page.

      Sometimes you need to hook up code with event handlers. For example, if there’s an event handler in the code, you still need to use the Visual Studio IDE to attach it to the appropriate controls.

      If you still can’t figure it out, zip up your project and email it to me and I’ll take a look.

Leave a Reply

Your email address will not be published. Required fields are marked *