[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: 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 the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.