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)
protected override void OnPaint(PaintEventArgs e)
For the painting, I used a method found in
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)
Rectangle bounds = new Rectangle(ClientRectangle.X,
ClientRectangle.Y + 6, ClientRectangle.Width,
ClientRectangle.Height - 6);
GroupBoxRenderer.DrawGroupBox(g, bounds, Enabled ?
GroupBoxState.Normal : GroupBoxState.Disabled);
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;
bounds.Y, i_endPos, bounds.Y);
using (SolidBrush drawBrush = new
SolidBrush(Color.FromArgb(0, 70, 213)))
g.DrawString(Text, this.Font, drawBrush, i_textPos, 0);
void DrawToggleButton(Graphics g)
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
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.