Click here to Skip to main content
Click here to Skip to main content
Go to top

Header-only GroupBox

, 21 Sep 2001
Rate this:
Please Sign up or sign in to vote.
Illustrates a simple subclass of a Windows.Forms.GroupBox control to paint only the header
<!-- Download Links --> <!-- Article image -->

Sample Image - headeronlygroupbox.gif

<!-- Main HTML starts here -->

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

Share

About the Author

Matthew Adams
Founder Endjin Ltd
United Kingdom United Kingdom
No Biography provided
Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralNice control but PinmemberXmen W.K.15-Feb-11 23:42 
GeneralBold font Pinmemberfredgate31-Aug-05 12:47 
GeneralProblem with Control Style Pinmemberphanf14-Apr-05 4:51 
GeneralDesign Mode PinmemberJohn Mancini24-Mar-03 9:56 
GeneralRe: Design Mode Pinsussmiliu11-Sep-03 16:38 
GeneralWhy not ControlPaint.DrawBorder3D Pinmembersentenza15-Jan-03 16:50 
GeneralRe: Why not ControlPaint.DrawBorder3D PinmemberMatthew Adams15-Jan-03 23:46 
QuestionBalloon Tips? PinmemberIripa30-Nov-01 14:11 
AnswerRe: Balloon Tips? PinmemberMatthew Adams14-Feb-02 12:25 
GeneralRe: Balloon Tips? PinmemberIripa15-Feb-02 15:51 
GeneralGreat! PinmemberJames T. Johnson30-Oct-01 21:30 
GeneralThanks, Matt! PinmemberChris Sells23-Sep-01 9:42 
GeneralRe: Thanks, Matt! PinmemberMatthew Adams23-Sep-01 10:15 

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 | Mobile
Web02 | 2.8.140916.1 | Last Updated 22 Sep 2001
Article Copyright 2001 by Matthew Adams
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid