This article from http://www.codeproject.com/KB/miscctrl/CollapsibleGroupBox.aspx
- Download project version 1.2-59.5 KB
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 usingPropertygrid
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 wocould create a pane containing controls (or anything for that matter) and add the panes toCscroller
. While searching codeproject for something suitable, I came monitoring SS rollup control and 3D Studio MAX like s0000able dialogbar, but both are written in C ++/MFC, and my project is in C #.
Using the code
After opening your C # windows application solution, openForm, 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\ Collapsiblegroupbox_1_2 \ bin \ releaseDirectory, and selectUitoolbox. 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 itemAdd \ add new item. Select a user control and edit its code. Change the base class fromSystem. Windows.FormS
ToUitoolbox. collapsiblegroupbox
, Save it, and then switch back to its design view.
At this point, you shoshould see something that looks like the following:
Feel free to resize it and add some controls.
Go back toFormYou wish to addExpandingpanel
To (in the design view), and addExpandingpanel
(From the "My User Controls" group of the Toolbox), And You shoshould see something like this:
Switch toForm'S code and add supporting code to addCollapsiblegroupbox
ToExpandingpanel
:
Collapse | Copy code
Public form 1 () { // // required for Windows form designer support // initializecomponent (); // usercontrol 1 is a collapsiblegroupbox expandingpanel1.addgroup ( New usercontrol 1 () ;}
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 want eCollapsiblegroupbox. 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 containGroupbox
Control. Instead, it 'fakes' a group box and draws it itself. Why? The first draft usedGroupbox
Control, and everything seemed to work fine. That is, until I created a panel derived fromExpandingpanel
, 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 nowCollapsiblegroupbox
Draws the group box itself.
Known issues
- Fonts-I 've only used the default 8.25pt Microsoft sans serif with the controls. It _ shoshould _ work correctly with any font, but it uses
Graphics. measurestring ()
To get the width, and I 've noticedMeasurestring ()
Doesn' t always work correctly.
-
Expandingpanel
S andCollapsiblegroupbox
Es are sometimes missing-ifFormContainsExpandingpanel
OrCollapsiblegroupbox
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 closeForm(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
Caption
AndContainstrashcan
Properties ofCollapsiblegroupbox
, 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 wocould be cool ifExpandingpanel
Supported drag and drop and allowed you to D & D withinExpandingpanel
To resort its items, or D & D to moveCollapsiblegroupbox
From oneExpandingpanel
To another. The UI wocould look something like this:
Add panes insteadCollapsiblegroupbox
ES: for my project, I only needed group boxes, but for a more general purpose, I might changeExpandingpanel
To add a more generic type (pane ?) :
Collapse | Copy code
Public ClassPane: system. Windows.FormS.Usercontrol{...}Public ClassCollapsiblegroupbox: pane {...}Public ClassExpandingpanel: system. Windows.FormS. Panel {...Public VoidAddgroup (uitoolbox. pane thepane ){...}...}
FAQ
History
- 25th June, 2004, 1.0
- 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
Groupbox
To a collapsed State.
- Removed commented code fromCollapsiblegroupbox. CS.