While working on a recent project, I had a need for a
GroupBox control that would enable/disable all of the controls within it. This type of control isn't built into the .NET Framework (which is a bit surprising), and when I tried to find an implementation here on CodeProject, I found one written in MFC but none which were written in .NET (C# or VB.NET), so I decided to write my own set of controls and release them here on CodeProject.
The controls themselves are pretty straight forward.
CheckGroupBox is a control derived from
System.Windows.Forms.GroupBox and has a
CheckBox added to the
GroupBox's child control collection (the
Controls property). Likewise,
RadioGroupBox is a control derived from
System.Windows.Forms.GroupBox and has a
RadioButton. Event handlers have been added so children within the
GroupBox reflect the check state of the
RadioButton, and parents of the
GroupBox control can be notified when the check state changes.
After opening your WinForm project, select "Add References..." and add UIToolbox.CheckGroupBox.dll and UIToolbox.RadioGroupBox.dll. Open the Toolbox window and navigate to the Containers group. You should now see
Simply drag and drop these controls onto your WinForm like you would any other control...
... and add controls to the
GroupBox like you would a standard
That's it. You're now ready to build and run your project.
The default enabling/disabling behavior of child controls is different between
RadioGroupBox. From the beginning, I knew that I wanted the child controls of a
CheckGroupBox to be enabled and disabled as the
CheckBox was checked, but when the
RadioButton of a
RadioGroupBox was selected (checked), I didn't want the enable state of the child controls to change.
This being CodeProject and all, I knew there would be some developers that wouldn't want this UI behavior so I added the
DisableChildrenIfUnchecked property to both controls.
DisableChildrenIfUnchecked defaults to
CheckGroupBox and defaults to
RadioGroupBox, and can be set in the designer at design-time.
RadioGroupBox Caveat #1: Mixing RadioButton Controls with RadioGroupBox Controls
If your WinForm contains
RadioGroupBox controls but not
RadioButton controls, you can just use
RadioGroupBox controls as-is directly within the WinForm like you can with
CheckGroupBox. However, if your WinForm has
RadioButton controls mixed in with
RadioGroupBox controls at the same level, you will run into a broadcasting issue. When a
RadioButton goes from being unchecked to checked, the
CheckGroupBox controls at that same level won't be notified of the check state change, and visa-versa.
To overcome this, I created a control derived from
RadioButtonPanel. To properly use
RadioGroupBox controls with
RadioButton controls, add a
RadioButtonPanel control to your WinForm before adding any
RadioButton controls or
RadioGroupBox controls. Then when you add
RadioButton controls or
RadioGroupBox controls, add them to the
RadioButtonPanel, not the root WinForm itself.
To see an example of this, open Form2.cs from the
RadioGroupBox Caveat #2: Adding RadioButton Controls to a RadioGroupBox
RadioButton you see in the
RadioGroupBox control is a child control of the
GroupBox. If you create an instance of a
RadioGroupBox control and view the instance's
Controls property (a controls collection) you will see that the collection contains the
If after adding a
RadioGroupBox to a WinForm, you were to add some
RadioButton controls, those too would be added to the
GroupBox's control collection. The problem with this is that since they are all within the same collection, selecting one of the
RadioButtons within the
GroupBox will unselect the
RadioButton that is meant to be the
To overcome this, before adding a
RadioButton control to a
RadioGroupBox, add a
Panel control to the
RadioGroupBox first, then add the
RadioButton controls to the Panel. With this technique, the
RadioButton controls are owned by the
Panel and not the
RadioGroupBox, so selecting one of them won't affect the
That's it. The controls are very straight forward and easy to use. Hopefully others out there can get some use from them. If you encounter any bugs, please send me an e-mail and I'll update the code.
This article and the accompanying files may be freely used provided the following conditions are met:
- The copyright statement in the files is not removed or modified.
- The code is not sold in uncompiled form (Released as a compiled binary which is part of an application is fine).
- The design, code, or compiled binaries are not "Re-branded".
- I receive credit in the About box of the released product (something along the lines of "CheckGroupBox and RadioGroupBox Copyright (c) 2009 Jeff Beeghly").
- I receive a fully licensed copy of the product (regardless of whether the product is free, shrinkwrap, or commercial). This is optional, though if you release products which use code I've contributed to, I would appreciate a fully licensed copy.
In addition, you may not:
- Publicly release modified versions of the code or publicly release works derived from the code without express written authorization from myself.
Q: Does a VB.NET version exist?
A: No, however the sources in this article's zip file contain compiled DLLs (UIToolbox.CheckGroupBox.dll and UIToolbox.RadioGroupBox.dll) which can be added to a VB.NET project as a reference.
Q: If I create a VB.NET version, may I release it?
A: No, that would violate the license.
Q: I've found a bug with the article's code. What should I do?
A: Either post the solution below in the message board or e-mail me the changes. I'll incorporate the changes and post a new version.
Q: I have a suggestion/enhancement/feature request. What should I do?
A: Either post the solution below in the message board or e-mail me the changes. If I decide the suggestion/enhancement/feature request should go in, I'll incorporate the changes and post a new version.
Q: Does a Visual Studio .NET 2008 version exist?
A: Yes, there are both 2005 and 2008 projects, though the controls were written with Visual Studio .NET 2005 and C# 2.0 and there isn't anything new in Visual Studio .NET 2008 or C# 3.5 which the controls take advantage of.
- January 22, 2009, 1.0
- January 23, 2009, 1.1
- Fixed the way text is handled so it appears correctly under Vista
Please ignore the following License section. CodeProject automatically adds it and there is no way to turn it off or set the license to "other". Please refer to the 'License' section above.