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

XP Style Collapsible GroupBox

, 31 Jan 2006
Rate this:
Please Sign up or sign in to vote.
This article describes an XP style collapsible GroupBox.

Sample Image


Sometimes, there is the need to display information in a container that can be collapsed. Sure there are controls like the XP Sidebar et all, but for my requirements, I wanted something that looked and worked exactly like a standard GroupBox. So, I simply extended the GroupBox control to provide this functionality.


I searched the web for existing controls and came across one by jeffb42 which was pretty much what I wanted, however I have been developing in .NET 2.0 only for some time now, and the old format design did not sit well with the normal GroupBoxes on my forms. Also, that version did not allow me to easily add other controls to the box for some reason. So, I created my own based on the one by jeffb42.

Using the code

This control is so simple that it barely warrants instructions. It can be used as a normal GroupBox is. Full design time support is provided so simply drag & drop the control onto your form and add what you wish to it.

Behind the control

Looking through the code, one can see that not much was required to create this control. The usual suspects were overridden to control the paint and the handling of the mouse clicking the Collapsed state toggle button.

protected override void OnMouseUp(MouseEventArgs e)
    if (m_toggleRect.Contains(e.Location))

protected override void OnPaint(PaintEventArgs e)

For the painting, I used a method found in System.Windows.Forms called DrawGroupBox. This takes away all need to draw arcs and lines or worry about matching the colour of the normal GroupBox. However, when the GroupBox text is drawn on this box, you will get a strikeout effect. To overcome this, I draw a line the same length as the measured text in the colour of the control so that it will not be seen.

void DrawGroupBox(Graphics g)
    // Get windows to draw the GroupBox
    Rectangle bounds = new Rectangle(ClientRectangle.X, 
       ClientRectangle.Y + 6, ClientRectangle.Width, 
       ClientRectangle.Height - 6);
    GroupBoxRenderer.DrawGroupBox(g, bounds, Enabled ? 
       GroupBoxState.Normal : GroupBoxState.Disabled);

    // Text Formating positioning & Size
    StringFormat sf = new StringFormat();
    int i_textPos = (bounds.X + 8) + m_toggleRect.Width + 2;
    int i_textSize = (int)g.MeasureString(Text, this.Font).Width;
    i_textSize = i_textSize < 1 ? 1 : i_textSize;
    int i_endPos = i_textPos + i_textSize + 1;

    // Draw a line to cover the GroupBox
    // border where the text will sit
    g.DrawLine(SystemPens.Control, i_textPos, 
               bounds.Y, i_endPos, bounds.Y);

    // Draw the GroupBox text
    using (SolidBrush drawBrush = new 
               SolidBrush(Color.FromArgb(0, 70, 213)))
        g.DrawString(Text, this.Font, drawBrush, i_textPos, 0);

void DrawToggleButton(Graphics g)
        g.DrawImage(, m_toggleRect);
        g.DrawImage(Properties.Resources.minus, m_toggleRect);

Points of Interest

If you are using this control in a DockStyle state of Fill then you will notice the control does not collapse. This is because the parent control forces the control to fill its space. Therefore, there is an event on the control which fires when the collapsed state changes. This can be used by the parent control to resize itself to the size of the GroupBox.


OK, after a request for a 1.1 version of this control, I thought I would give it a shot. After not using Visual Studio 2003 for around a year, it was a shock to switch back to it. Now, a lot of the methods that I used in the 2.0 solution were not available in 1.1, especially drawing a rounded rectangle. Rather than calculating all the points and curves myself, I looked through CodeProject because surely someone had done it before. And of course someone has, Arun Reginald's article provided a simple solution.

The rest is pretty much the same as in the 2.0 solution. I used Barretto VN's idea to add XP theming to all the controls used in the project.


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


About the Author

Web Developer
United Kingdom United Kingdom
I write code.

Comments and Discussions

QuestionNicely done [modified] PinmemberMike Hankey1-Jun-12 8:25 
QuestionBug with AutoSizeMode, but very good control. Pinmemberthilitium27-May-12 13:26 
QuestionThanks for the great code!! One question though ... PinmemberMarek.bucki21-Feb-11 1:46 
QuestionError when calling IsCollapsed from code? Pinmemberobcstf27-Aug-10 5:22 
AnswerRe: Error when calling IsCollapsed from code? Pinmemberobcstf27-Aug-10 7:16 
GeneralBroken link PinmemberWolexie22-Jan-10 5:48 
QuestionGreat Control but is there a way to disable or hide border Pinmemberbokee1127-Oct-09 8:12 
AnswerRe: Great Control but is there a way to disable or hide border Pinmemberbokee1127-Oct-09 10:21 
GeneralSome minor changes... PinmemberGMan49114-Oct-09 8:27 
GeneralRe: Some minor changes... PinmemberDominikF8-Dec-09 2:19 
QuestionCode Usage - Article Pinmemberstixoffire18-May-09 6:51 
GeneralLicense PinmemberAlmagnus13-Mar-09 14:40 
GeneralInvisible controls Pinmemberpl_meetom8-Dec-08 4:09 
GeneralRe: Invisible controls Pinmemberandrea@gmi24-Sep-10 9:08 
GeneralThe test program crashes if you run it with the "Windows Classic" theme on XP [modified] Pinmemberbscaer10-Jun-08 6:38 
GeneralResize parents [modified] Pinmemberesskar17-Sep-07 3:52 
QuestionRe: Resize parents [modified] Pinmemberstixoffire10-Jun-08 10:08 
AnswerRe: Resize parents Pinmembertretererte29-Oct-09 8:37 
GeneralI like it, small bug (small small) [modified] Pinmemberbilo814-May-07 3:28 
GeneralRe: I like it, small bug (small small) PinmemberRoss Korsky6-Jun-08 7:22 
GeneralRe: I like it, small bug (small small) Pinmembermaynardflies15-Aug-08 5:28 
NewsDesign-time enhancement PinmembercDima17-Apr-07 3:48 
GeneralRe: Design-time enhancement Pinmemberstixoffire10-Jun-08 10:10 
QuestionRe: Design-time enhancement Pinmemberstixoffire18-Apr-09 1:28 
GeneralLocalization messes up the control PinmemberBo K8-Feb-07 8:12 
GeneralCollapse box look PinmemberE! Ray K29-Jan-07 9:52 
QuestionFor some reason.. Pinmemberadvance51231-Aug-06 8:07 
GeneralCustomize colors PinmemberRogenator_202415-Jul-06 14:35 
GeneralGreat Component! PinmemberMikeNutshell16-Jun-06 14:06 
GeneralRe: Great Component! PinmemberRogenator_202411-Jul-06 15:40 
GeneralRight to left PinmemberAhmad Maatouki2-Apr-06 0:44 
GeneralGood Work PinmemberSyed Uzair Ahsan12-Mar-06 1:48 
Generalbox disappears! PinmemberDarchangel10-Feb-06 9:48 
GeneralRe: box disappears! Pinmemberjordanhammond14-Feb-06 5:15 
GeneralRe: box disappears! PinmemberNic Oughton9-May-06 7:16 
RantRe: box disappears! PinmemberSentax8-Jan-09 7:47 
Generalquestion about box behind text PinmemberDarchangel9-Feb-06 2:56 
GeneralRe: question about box behind text Pinmemberjordanhammond9-Feb-06 3:00 
GeneralRe: question about box behind text PinmemberDarchangel10-Feb-06 9:40 
GeneralRe: question about box behind text Pinmemberuasking14-Feb-06 5:00 
GeneralRe: question about box behind text Pinmemberjordanhammond14-Feb-06 5:21 
GeneralExcellent control dude. Pinmembermrsnipey1-Feb-06 18:56 
GeneralStarting collapsed will lead to a wrong size Pinmembermikeeblau1-Feb-06 1:53 
GeneralRe: Starting collapsed will lead to a wrong size Pinmemberjordanhammond14-Feb-06 5:20 
GeneralRe: Starting collapsed will lead to a wrong size Pinmembergis_wgh29-Mar-07 18:38 
GeneralRe: Starting collapsed will lead to a wrong size Pinmemberbadimo24-Oct-07 7:08 
GeneralGlyphs Pinmemberuasking1-Feb-06 0:32 
GeneralRe: Glyphs Pinmemberjordanhammond1-Feb-06 0:34 
QuestionRe: Glyphs Pinmemberlukasz_sw8-Feb-06 3:13 
AnswerRe: Glyphs Pinmemberuasking8-Feb-06 23:40 

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 | Terms of Use | Mobile
Web01 | 2.8.150224.1 | Last Updated 31 Jan 2006
Article Copyright 2006 by jordanhammond
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid