Click here to Skip to main content
15,895,084 members
Articles / Programming Languages / Visual Basic

Customising the .NET Panel Control

Rate me:
Please Sign up or sign in to vote.
4.69/5 (52 votes)
15 Sep 2004CPOL3 min read 394.6K   10.3K   143   111
How to customise the .NET Panel control to give it a gradient fill, and round corners

  

Sample Image - custompanel.png

Introduction

Whenever I use a Windows Panel, the first thing that annoys me with the default .NET Panel is the inability to set the border color. This is closely followed by the lack of gradients and rounded corners. As you can see from the screen shot above, a gradient fill with rounded corners makes for a very simple imitation of an XP style command button.

Update

Since various people have asked me for Fixed3D borderStyle support, and I have found a number of small, but annoying problems with the positioning of this panel control, I have updated the code. However, it was not easy. Setting the borderStyle to Fixed3D has the nasty effect off adding a 3D border to all the children of the control! Consequently, this version includes a method to draw my own 3D border. When using this style, the panel will always be rectangular.

Background

The easiest way to enhance a control is by visual inheritance, although there are one or two things to watch out for.

There is a big difference between Overriding a property and Shadowing a property. If you override a property, then any code in the base class which uses that property will call the code in the inherited class, whereas shadowing a property leaves the base class calling its own code and the shadowed property will only be available in the inherited class.

In C#, the override modifier is used instead of the VB Overrides modifier, and the new modifier is used instead of the VB Shadows modifier.

Some controls do not allow transparent backgrounds. Fortunately, the Windows Panel control does.

One very useful property of all controls is DesignMode. This can be used to discover if the control is in a run-time, or design-time environment.

The Solution

Some of the important parts of the solution are as follows.

We must ensure that transparent backgrounds are supported, and that we have full control of painting the control by adding the following code to the constructor:

VB
Me.SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer, True)
Me.SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, False)
Me.SetStyle(System.Windows.Forms.ControlStyles.ResizeRedraw, True)
Me.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, True)
Me.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, True)
MyBase.BackColor = System.Drawing.Color.Transparent

ResizeRedraw makes sure that the control will repaint itself if you stretch it, and DoubleBuffer makes the drawing happen off-screen so as to reduce flickering.

Always remember to dispose off drawing objects as they wrap the unmanaged GDI+ API and will chew up resources if left hanging.

Using the Code

As you will see, in the demo solution, it is very easy to make the panels 'Hot' by swapping the colors over on mouse enter and leave.

To use the control, make sure you have the control and the enum in your solution, build it, add the custom panel to your Toolbox, and away you go. The new or enhanced properties are:

  • BackColor - Shadows the basic BackColor property
  • BackColor2 - The second color of the gradient background
  • GradientMode - The direction of the gradient (defaults to none)
  • BorderStyle - Only the FixedSingle works in this sub-class
  • BorderWidth - Controls the width of the border
  • BorderColor - The color of the border
  • Curvature - The radius of the curve used to draw the corners of the panel
  • CurveMode - The style of the curves to be drawn on the control

History

  • 6th July, 2004: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
Unfortunately my real name was already in use as a code project login. For those of you who are wondering I am really Napoleon Solo. Sorry, I mean, Mark Jackson. Well, I do look a bit like him I think.

Comments and Discussions

 
QuestionInsightful Contents Pin
jokefan13-May-14 15:51
jokefan13-May-14 15:51 
BugScrolling problem when AutoScroll is true Pin
aletsan1-Sep-13 9:14
aletsan1-Sep-13 9:14 
Nice and simple custom panel, thanks.

I have two minor fixes for you:

When I set AutoScroll to true, add some controls to activate the scroll bars and use the scroll bars, I got painting problems. The side border lines in the scrolling direction were showing inside the panel and the border did not redraw correctly.

The fix was very easy of course, just override OnScroll and invalidate the panel:
C#
protected override void OnScroll(System.Windows.Forms.ScrollEventArgs se)
{
   base.OnScroll(se);
   this.Invalidate();
}


Sure enough a second problem appeared, as invalidating the panel means redrawing it and this introduced some flickering, especially in the border. It seems like:
C#
this.SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer, true);

doesn't always work as expected (as usual...).

The fix for that was easy and fast as well. Override the CreateParams of the control. It never fails to do the trick:
C#
protected override System.Windows.Forms.CreateParams CreateParams
{
    get
    {
	// Original code returns CreateParams.
	// New code returns CreateParams
	// with extended window style WS_EX_COMPOSITED turned on.
	// It helps with flickering in simple problems.
	
	//return base.CreateParams;
	System.Windows.Forms.CreateParams cp = base.CreateParams;
    	cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
    	return cp;
    }
}


With these 2 little additions, and no code checking/adding/changing, the scroll problem is fixed. I always prefer to do it this way when there is no real reason to dive into other peoples code.

Of course we can always turn on WS_EX_COMPOSITED for the entire form using the exact same code (maybe thats the reason no one caught this little problem earlier), but keeping the fix inside the custom control fixes it for good.
GeneralRe: Scrolling problem when AutoScroll is true Pin
Southmountain12-Dec-15 10:39
Southmountain12-Dec-15 10:39 
QuestionEasiest custom panel Pin
Kartik Goyal18-May-13 22:38
professionalKartik Goyal18-May-13 22:38 
Questiongood Pin
yannKemp13-Sep-12 2:29
yannKemp13-Sep-12 2:29 
QuestionGreat Code Pin
Elizalde G. Baguinon22-Jul-12 17:42
Elizalde G. Baguinon22-Jul-12 17:42 
GeneralPlease reinstate your downloads for this tantalizing component! Pin
terryd25-Aug-10 10:23
terryd25-Aug-10 10:23 
GeneralDownload Links seem dead Pin
byytas13-Aug-10 1:13
byytas13-Aug-10 1:13 
GeneralRe: Download Links seem dead Pin
The Man from U.N.C.L.E.23-Aug-10 0:06
The Man from U.N.C.L.E.23-Aug-10 0:06 
GeneralRe: Download Links seem dead Pin
Chris Maunder16-Feb-11 1:40
cofounderChris Maunder16-Feb-11 1:40 
GeneralRe: Download Links seem dead Pin
The Man from U.N.C.L.E.16-Feb-11 1:41
The Man from U.N.C.L.E.16-Feb-11 1:41 
GeneralTransparent controls Pin
jdkl5415-Jun-10 2:08
jdkl5415-Jun-10 2:08 
GeneralIncorrect redrawing when scrolling Pin
crypto_rsa28-May-10 1:25
crypto_rsa28-May-10 1:25 
GeneralRe: Incorrect redrawing when scrolling Pin
The Man from U.N.C.L.E.28-May-10 1:40
The Man from U.N.C.L.E.28-May-10 1:40 
GeneralUnable to download code Pin
hswear38-Apr-10 15:33
hswear38-Apr-10 15:33 
GeneralRe: Unable to download code Pin
hswear38-Apr-10 15:35
hswear38-Apr-10 15:35 
QuestionError BC30002 - Type MyApp.CustomPanel is not defined Pin
MarioRDJ5-Apr-10 10:09
MarioRDJ5-Apr-10 10:09 
QuestionFlickering Issue Pin
PareshM22-Jan-09 2:31
PareshM22-Jan-09 2:31 
AnswerRe: Flickering Issue Pin
Yann Romefort4-Mar-09 22:10
Yann Romefort4-Mar-09 22:10 
GeneralMine works great! Thanks for the code! Pin
234wer28-Oct-08 15:22
234wer28-Oct-08 15:22 
GeneralC++ /CLI Pin
Xaria14-Oct-08 19:01
Xaria14-Oct-08 19:01 
AnswerRe: C++ /CLI Pin
The Man from U.N.C.L.E.14-Oct-08 22:46
The Man from U.N.C.L.E.14-Oct-08 22:46 
QuestionBlack edges Pin
laith_b29-Apr-08 10:23
laith_b29-Apr-08 10:23 
QuestionProgrammatically created CustomPanel is not transparent Pin
DustinDS24-Aug-07 5:16
DustinDS24-Aug-07 5:16 
GeneralExcellent component! Pin
philnad13-Jul-07 7:41
philnad13-Jul-07 7:41 

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.