Click here to Skip to main content
Click here to Skip to main content

CheckGroupBox and RadioGroupBox

, 22 Jan 2009
Rate this:
Please Sign up or sign in to vote.
An introduction to the CheckGroupBox, RadioGroupBox, and RadioButtonPanel controls

Introduction

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.

Implementation

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 CheckBox or RadioButton, and parents of the GroupBox control can be notified when the check state changes.

Getting Started

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 CheckGroupBox and RadioGroupBox listed: 

Toolbox.gif

Simply drag and drop these controls onto your WinForm like you would any other control...

NewCheckGroupBox.gif

... and add controls to the GroupBox like you would a standard GroupBox:

NewCheckGroupBoxWithControls.gif

That's it. You're now ready to build and run your project.

DisableChildrenIfUnchecked  

The default enabling/disabling behavior of child controls is different between CheckGroupBox and 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 true for CheckGroupBox and defaults to false for 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 System.Windows.Forms.Panel called 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's ExampleApplication project:

RadioButtonPanelExample.gif

RadioGroupBox Caveat #2: Adding RadioButton Controls to a RadioGroupBox

The RadioButton you see in the RadioGroupBox control is a child control of the RadioGroupBox's 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 RadioButton.

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 GroupBox's caption RadioButton.

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 RadioGroupBox's caption RadioButton.

Summary

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.

License

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".

Optional:

  • 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. 

FAQ

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.  

History

  • January 22, 2009, 1.0
    • Initial release
  • 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. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

jeffb42
Software Developer
United States United States
In a nutshell, my forte is Windows, Macintosh, and cross-platform development, and my interests are in UI, image processing, and MIDI application development.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberHalil ibrahim Kalkan27-Dec-11 1:46 
GeneralMy vote of 4 Pinmembertr021726-Sep-10 22:50 
GeneralBug, Both CheckGroupBox and RadioGroupBox. Pinmembersa222222229-Jul-09 1:46 
GeneralRe: Bug, Both CheckGroupBox and RadioGroupBox. Pinmemberjeffb4229-Jul-09 8:13 
GeneralRe: Bug, Both CheckGroupBox and RadioGroupBox. Pinmembersa222222229-Jul-09 21:01 
GeneralRe: Bug, Both CheckGroupBox and RadioGroupBox. Pinmemberjeffb4230-Jul-09 14:58 
GeneralBug in RadioGroupBox Constructor Pinmembervvvkkkggg23-Jul-09 14:35 
GeneralRe: Bug in RadioGroupBox Constructor Pinmemberjeffb4225-Jul-09 13:30 
GeneralNice paradigm to problem Pinmemberkonikula27-Jan-09 12:14 
GeneralGreat! I was waiting for it... PinmemberCreF22-Jan-09 20:30 
GeneralJust what I needed Pinmembermark_sand22-Jan-09 17:37 
GeneralFix for Vista Pinmemberjeffb4222-Jan-09 16:23 
GeneralVista bug Pinmemberjeffb4222-Jan-09 13:13 
GeneralNice PinmemberMarc Leger22-Jan-09 10:41 
GeneralRe: Nice Pinmemberjeffb4222-Jan-09 13:15 
GeneralPerfect timing PinmemberAdrian Cole22-Jan-09 10:33 
GeneralRe: Perfect timing Pinmemberjeffb4222-Jan-09 13:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 22 Jan 2009
Article Copyright 2009 by jeffb42
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid