Click here to Skip to main content
Licence 
First Posted 21 Sep 2001
Views 97,329
Bookmarked 36 times

Header-only GroupBox

By | 21 Sep 2001 | Article
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

About the Author

Matthew Adams

Chief Technology Officer
Ythos Ltd
United Kingdom United Kingdom

Member

I've been developing software on the Microsoft platform for over 15 years. I'm currently focusing on the .NET 3.5 stack, with a particular interest in healthcare and life sciences applications.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNice control but PinmemberXmen W.K.23:42 15 Feb '11  
GeneralBold font Pinmemberfredgate12:47 31 Aug '05  
GeneralProblem with Control Style Pinmemberphanf4:51 14 Apr '05  
GeneralDesign Mode PinmemberJohn Mancini9:56 24 Mar '03  
GeneralRe: Design Mode Pinsussmiliu16:38 11 Sep '03  
GeneralWhy not ControlPaint.DrawBorder3D Pinmembersentenza16:50 15 Jan '03  
GeneralRe: Why not ControlPaint.DrawBorder3D PinmemberMatthew Adams23:46 15 Jan '03  
QuestionBalloon Tips? PinmemberIripa14:11 30 Nov '01  
AnswerRe: Balloon Tips? PinmemberMatthew Adams12:25 14 Feb '02  
GeneralRe: Balloon Tips? PinmemberIripa15:51 15 Feb '02  
GeneralGreat! PinmemberJames T. Johnson21:30 30 Oct '01  
GeneralThanks, Matt! PinmemberChris Sells9:42 23 Sep '01  
GeneralRe: Thanks, Matt! PinmemberMatthew Adams10:15 23 Sep '01  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 22 Sep 2001
Article Copyright 2001 by Matthew Adams
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid