Make a GroupBox that uses a CheckBox in its caption to determine whether its items are enabled in C#

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

The CheckBox's Click event handler calls the ManageCheckFrame function, which does all of the interesting work.

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

// Get the appropriate color for the contained controls.
Color fore_color;
if (grp.Enabled)
{
fore_color = Color.FromKnownColor(KnownColor.ActiveCaptionText);
}
else
{
fore_color = Color.FromKnownColor(KnownColor.InactiveCaptionText);
}

// Color the controls in the GroupBox.
foreach (Control ctl in grp.Controls)
{
ctl.ForeColor = fore_color;
}
}

Unfortunately if the CheckBox is inside the GroupBox, then when the CheckBox disables the GroupBox it also disables itself so the user won't be able to re-check the CheckBox. Complicating matters is the fact that the Form Designer tends to drop the CheckBox inside the GroupBox if you place the CheckBox over the GroupBox, so it's hard to arrange them nicely without putting one inside the other.

To handle this problem, the ManageCheckGroupBox function checks whether the CheckBox is inside the GroupBox and, if it is, the function moves it into the GroupBox's parent. It adjusts the CheckBox's location so it doesn't move on the screen and moves it to the front of the stacking order so it doesn't end up behind the GroupBox.

Next the code enables or disables the GroupBox (which enables or disables all of the controls it contains). It then gives the GroupBox's controls the proper color (normally black or gray) to indicate whether they are disabled.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

  • 2/24/2010 8:54 PM slearl wrote:
    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.

    http://i47.tinypic.com/i20rwy.png
    Reply to this
    1. 2/25/2010 7:46 AM Rod Stephens wrote:
      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.

      Reply to this
  • 3/8/2011 5:58 AM Paula wrote:
    Muchas gracias por el aporte! excelente!
    Reply to this
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.