Here's an Expandable Groupbox control. It works just like the regular GroupBox control, except that there is an Expand Button next to the caption. Clicking that button will Expand or Collapse the Groupbox.
Screenshot when expanded:
And after collapsing:
The control has the following new Properties you can set:
- Caption - Sets the text of the caption,
- BorderColor - Sets the color of the custom-drawn border,
- CaptionColor - Sets the color of the caption text,
- Expanded - Whether or not the control is expanded,
- ExpandedSize - The size of the control when expanded,
- HeaderClickExpand - Whether or not the user can click the header (caption) to expand/collapse the control.
The control can be expanded / collapsed during Design-Time simply by clicking the Expand Button. Yes, it works during Design-Time
That's all you need to know if you want to use it
If you want some more information, read on.
The control is simply a UserControl with two panels: the header panel and the content panel. In the header panel, I put a Button and a Label.
The + and - signs on the button are custom drawn, because it is too small to display its normal caption text properly.
The rounded border is drawn in parts. First, the top part and a small part of the vertical lines is drawn on the header panel, and the rest is drawn on the content panel separately.
There is no groupbox in this control at all, and no inheriting from the groupbox. It only looks like a groupbox because I've custom drawn it that way.
For this reason, it is completely customizable (at least, if you want to put some effort into it ). You could make it look however you want by editing the painting methods. You are not stuck with the groupbox appearance.
To enable the button to work during Design-Time, I had to write a Designer class. The GetHitTest function in that class actually enables you to click the button.
Once you button is clicked, I also need to tell the Visual Studio designer that the control has updated (it's size has changed). If it doesn't do this, then the selection rectangle remains in the old size until you re-select it, which is quite confusing. To make that happen, I get a reference to the control and add the ExpandButton_Click event handler, so I can notify the designer when it is clicked.
Sorry, can't post the full code as it was too long to fit in a single post...
VB file is attached.
1. Add a reference to System.Design (required to compile!) Huh?! What's that?
2. Add the attached VB file to your project.
3. Build your project.
Updated to fix an error with the caption text resetting. The Text property is now renamed to the Caption property.