Click here to Skip to main content
15,867,756 members
Articles / Programming Languages / C#
Article

Header-only GroupBox

Rate me:
Please Sign up or sign in to vote.
4.63/5 (8 votes)
21 Sep 2001MIT1 min read 126.3K   2K   36   13
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, along with any associated source code and files, is licensed under The MIT License


Written By
Founder Endjin Ltd
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNice control but Pin
Xmen Real 15-Feb-11 23:42
professional Xmen Real 15-Feb-11 23:42 
GeneralBold font Pin
fredgate31-Aug-05 12:47
fredgate31-Aug-05 12:47 
GeneralProblem with Control Style Pin
phanf14-Apr-05 4:51
phanf14-Apr-05 4:51 
GeneralDesign Mode Pin
John Mancini24-Mar-03 9:56
John Mancini24-Mar-03 9:56 
GeneralRe: Design Mode Pin
Ming Liu11-Sep-03 16:38
Ming Liu11-Sep-03 16:38 
GeneralWhy not ControlPaint.DrawBorder3D Pin
sentenza15-Jan-03 16:50
sentenza15-Jan-03 16:50 
GeneralRe: Why not ControlPaint.DrawBorder3D Pin
Matthew Adams15-Jan-03 23:46
professionalMatthew Adams15-Jan-03 23:46 
QuestionBalloon Tips? Pin
Iripa30-Nov-01 14:11
Iripa30-Nov-01 14:11 
AnswerRe: Balloon Tips? Pin
Matthew Adams14-Feb-02 12:25
professionalMatthew Adams14-Feb-02 12:25 
GeneralRe: Balloon Tips? Pin
Iripa15-Feb-02 15:51
Iripa15-Feb-02 15:51 
GeneralGreat! Pin
James T. Johnson30-Oct-01 21:30
James T. Johnson30-Oct-01 21:30 
GeneralThanks, Matt! Pin
Chris Sells23-Sep-01 9:42
Chris Sells23-Sep-01 9:42 
I use this control in my own work [1]. BTW, if you want to know about how to integrate with the forms designer, check out [2].

[1] http://www.sellsbrothers.com/samples/RegexPlorer.jpg
[2] http://www.codeproject.com/netcomponents/propertytree.asp

Chris Sells
http://www.develop.com/genx/
http://www.sellsbrothers.com/
GeneralRe: Thanks, Matt! Pin
Matthew Adams23-Sep-01 10:15
professionalMatthew Adams23-Sep-01 10:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.