Click here to Skip to main content
15,881,898 members
Articles / Programming Languages / C#
Article

Collapsible Group Box

Rate me:
Please Sign up or sign in to vote.
4.42/5 (21 votes)
5 Jan 20064 min read 174.4K   4.3K   71   30
An introduction to the ExpandingPanel and CollapsibleGroupBox controls.

Image 1

Introduction

I am working on a project that needs to display groups/containers of editable data (data not just for display), and I came up with the idea for this library of controls. I looked into using a PropertyGrid control but it didn't have the elegance I was looking for, so here are the fruits of my labor.

Background

Years ago, I worked on a project for the Macintosh using Symantec's TCL (Think Class Library, not Tcl/Tk). TCL has a view and pane architecture where one would create a pane containing controls (or anything for that matter) and add the panes to a CScroller. While searching CodeProject for something suitable, I came across Rollup Control and 3D Studio Max like Slidable DialogBar, but both are written in C++/MFC, and my project is in C#.

Using the code

After opening your C# Windows Application solution, open a Form, right-click in the Toolbox, and select the "Add/Remove Items..." menu item. After the "Customize Toolbox" dialog comes up, select the "Browse..." button, and navigate to the \CollapsibleGroupBox_1_2\bin\Release directory, and select UIToolbox.dll. If you navigate to the "My User Controls" group in the Toolbox, you will notice that several items have been added.

Go back to the Solution Explorer, right-click on your project, and select the menu item Add\Add New Item. Select a user control and edit its code. Change the base class from System.Windows.Forms to UIToolbox.CollapsibleGroupBox, save it, and then switch back to its design view.

At this point, you should see something that looks like the following:

Image 2

Feel free to resize it and add some controls.

Go back to the form you wish to add an ExpandingPanel to (in the design view), and add an ExpandingPanel (from the "My User Controls" group of the Toolbox), and you should see something like this:

Image 3

Switch to the form's code and add supporting code to add the CollapsibleGroupBox to the ExpandingPanel:

C#
public Form1()
 {
     //
     // Required for Windows Form Designer support
     //
     InitializeComponent();

     // UserControl1 is a CollapsibleGroupBox
     expandingPanel1.AddGroup(new UserControl1());
 }

That's it. You're ready to build and run it now. The controls automatically take care of collapsing, expanding, deleting themselves, and scrolling.

Points of interest

If you explore CollapsibleGroupBox.cs, you will notice that although it contains a control for the collapse button and a control for the trash can button, it doesn't contain a GroupBox control. Instead, it 'fakes' a group box and draws it itself. Why? The first draft used a GroupBox control, and everything seemed to work fine. That is, until I created a panel derived from ExpandingPanel, added some controls, and then built it. When the project was built, the derived panel seemed to 'eat' the controls. It turns out that the bug had to do with the embedded group box and the ownership of the controls, so now the CollapsibleGroupBox draws the group box itself.

Known issues

  • Fonts - I've only used the default 8.25pt Microsoft Sans Serif with the controls. It __should__ work correctly with any font, but it uses Graphics.MeasureString() to get the width, and I've noticed MeasureString() doesn't always work correctly.
  • ExpandingPanels and CollapsibleGroupBoxes are sometimes missing - If a form contains an ExpandingPanel or CollapsibleGroupBox control, I've noticed that sometimes they disappear the next time I open the project. A workaround that I've found that usually works is to close the form (both designer and code) and then reopen it. I haven't figured out why, but this seems to fix the problem.
  • Trash can layout - When playing with the Caption and ContainsTrashCan properties of a CollapsibleGroupBox, I've noticed that the trash can icon doesn't always appear in the correct position. Usually building the project solves this.

Future enhancements

Here are some potential enhancements I may make in the future:

Drag & Drop: It would be cool if ExpandingPanel supported drag and drop and allowed you to d&d within an ExpandingPanel to resort its items, or d&d to move a CollapsibleGroupBox from one ExpandingPanel to another. The UI would look something like this:

Image 4

Add panes instead of CollapsibleGroupBoxes: For my project, I only needed group boxes, but for a more general purpose, I might change ExpandingPanel to add a more generic type (Pane?):

C#
public class Pane : System.Windows.Forms.UserControl
{
    .
    .
    .
}

public class CollapsibleGroupBox : Pane
{
    .
    .
    .
}


public class ExpandingPanel : System.Windows.Forms.Panel
{
    .
    .
    .

    public void AddGroup(UIToolbox.Pane thePane)
    {
        .
        .
        .
    }
    .
    .
    .
}

FAQ

  • I would like to modify and use your code. Are there any restrictions I need to know about, or can I just build on your code at will?

    You may use and modify the original code to your needs, free of charge, provided:

    • I received credit in the products' about box (something along the lines of "Collapsible Group Box by Jeff Beeghly").
    • (Optional) I receive a fully licensed copy of the product (regardless of whether the product is free, shrinkwrap, or commercial).

    You may not:

    • Sell the code (just the code, not as part of an application).
    • "Re-brand" the code as your own.
    • Publicly release modified versions of the code or publicly release works derived from the code without express written authorization.

History

  • 25th June, 2004, 1.0
    • Initial release
  • 3rd January, 2006, 1.2
    • Built with Visual Studio .NET 2003.
    • Added monkfinger's fix for resizing (see message board).
    • Added Doug Case's request to programmatically set a GroupBox to a collapsed state.
    • Removed commented code from CollapsibleGroupBox.cs.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
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

 
QuestionNo components found in DLL Pin
ecureuill28-Oct-15 19:47
ecureuill28-Oct-15 19:47 
QuestionTo add Controls Pin
Ravndra2211-Aug-13 17:58
Ravndra2211-Aug-13 17:58 
GeneralMy vote of 5 Pin
neoraltech13-Mar-13 15:11
neoraltech13-Mar-13 15:11 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey21-Feb-12 0:20
professionalManoj Kumar Choubey21-Feb-12 0:20 
QuestionI'd like to modify this code... Pin
Marc Clifton5-Jan-12 4:06
mvaMarc Clifton5-Jan-12 4:06 
AnswerRe: I'd like to modify this code... Pin
jeffb425-Jan-12 17:30
jeffb425-Jan-12 17:30 
GeneralRe: I'd like to modify this code... Pin
Marc Clifton6-Jan-12 3:00
mvaMarc Clifton6-Jan-12 3:00 
GeneralAdd items in the developer environement Pin
Zipadie Doodah31-Mar-11 9:00
Zipadie Doodah31-Mar-11 9:00 
GeneralDrag and Drop Pin
Evellyn16-Apr-09 4:46
Evellyn16-Apr-09 4:46 
GeneralPossible bug or my usage is wrong. Pin
BKail1-Apr-09 10:36
BKail1-Apr-09 10:36 
GeneralRe: Possible bug or my usage is wrong. Pin
jeffb426-Apr-09 6:51
jeffb426-Apr-09 6:51 
Generalrequest 2.0 version Pin
uzay9113-Jul-07 4:03
uzay9113-Jul-07 4:03 
GeneralRe: request 2.0 version Pin
jeffb426-Jul-07 13:44
jeffb426-Jul-07 13:44 
Generalcollapsible groupbox not acting like a container Pin
Darchangel10-Feb-06 8:27
Darchangel10-Feb-06 8:27 
GeneralCollapsible Group Box version 1.2 Pin
jeffb426-Jan-06 5:33
jeffb426-Jan-06 5:33 
Version 1.2 of the Collapsible Group Box article has just been posted. Differences between 1.2 and 1.1 include:
* Added resizing fix
* Added ability to programmatically set a GroupBox to a collapsed state

Enjoy!


Jeff

GeneralRe: Collapsible Group Box version 1.2 Pin
shengcheng_jin8-Jul-08 11:26
shengcheng_jin8-Jul-08 11:26 
GeneralInitial state Pin
dougcase@microsoft25-Oct-05 8:24
dougcase@microsoft25-Oct-05 8:24 
GeneralGlitch Fix: CollapsibleGroupBox Resize Pin
monkfinger2-May-05 13:52
monkfinger2-May-05 13:52 
QuestionLicense? Pin
Justin F.13-Jan-05 9:46
Justin F.13-Jan-05 9:46 
AnswerRe: License? Pin
jeffb4213-Jan-05 14:12
jeffb4213-Jan-05 14:12 
GeneralRe: License? Pin
Justin F.13-Jan-05 16:07
Justin F.13-Jan-05 16:07 
Generalwrong download link Pin
Dennis Nusser28-Jun-04 5:47
Dennis Nusser28-Jun-04 5:47 
GeneralRe: wrong download link Pin
jeffb4228-Jun-04 6:27
jeffb4228-Jun-04 6:27 
GeneralOne little quirk Pin
mav.northwind26-Jun-04 20:39
mav.northwind26-Jun-04 20:39 
GeneralRe: One little quirk Pin
jeffb4227-Jun-04 9:50
jeffb4227-Jun-04 9:50 

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

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