Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Header-only GroupBox

0.00/5 (No votes)
21 Sep 2001 1  
Illustrates a simple subclass of a Windows.Forms.GroupBox control to paint only the header

Sample Image - headeronlygroupbox.gif

Introduction

This is a simple example of a control derived from a Windows.Forms.GroupBox class, to provide the (now fairly standard) 'boxless' separator, of which there are countless MFC examples out there.

The only thing that you might not have come across before is the use of the ControlPaint class.

HeaderOnlyGroupbox

Firstly, it is used to paint disabled text for the control:

if( Enabled )
{
	Brush br = new SolidBrush( ForeColor );
	e.Graphics.DrawString( Text, Font, br, ClientRectangle, format );
	br.Dispose();
}
else
{
	ControlPaint.DrawStringDisabled( e.Graphics, Text, Font, BackColor,
	                                 ClientRectangle, format );
}

Then, it is used again to get the Dark and LightLight control colors, relative to the current BackColor:

Pen forePen = new Pen( ControlPaint.LightLight( BackColor ), SystemInformation.BorderSize.Height );
Pen forePenDark = new Pen( ControlPaint.Dark( BackColor ), SystemInformation.BorderSize.Height );

That's pretty much all there is to it.

Using the class

In your own code it is just a drop-in replacement for GroupBox

If you're using the VS.NET IDE it isn't quite so simple (at least, not for me). I can't find a way to persuade the ToolBox to recognize the control as something it understands. If anyone has any ideas, then drop me a line.

However, it is easy enough to work around. Just drop a regular GroupBox down on your form. Then, switch to the code editor, and replace the 2 instances of GroupBox with HeaderGroupBox (not forgetting to qualify the namespace appropriately). You can carry on using the Forms editor, and it picks up our new rendering just fine.

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