Click here to Skip to main content
Click here to Skip to main content

Customising the .NET Panel control

By , 15 Sep 2004
 

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:

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.

License

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

About the Author

The Man from U.N.C.L.E.
Software Developer
United Kingdom United Kingdom
Member
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.

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionEasiest custom panelmemberKartik Goyal18 May '13 - 22:38 
Thx...this is what I was looking for.
QuestiongoodmemberyannKemp13 Sep '12 - 2:29 
very nice code i love it ...
QuestionGreat CodememberElizalde G. Baguinon22 Jul '12 - 17:42 
Simple but robust!
ChumCircle Flash Games - http://www.chumcircle.co.cc

GeneralPlease reinstate your downloads for this tantalizing component!memberterryd25 Aug '10 - 10:23 
Hello:
 
This is a really great component. Please can you post us a link to the software.
 
Thank you and all the best!
Smile | :)
GeneralDownload Links seem deadmemberbyytas13 Aug '10 - 1:13 
When I click on either of these two download links, it just opens up a blank page. Refreshing doesn't help. Doesn't seem browser related either (tried few different ones). Would it be possible to re-post the downloads?
GeneralRe: Download Links seem deadmemberThe Man from U.N.C.L.E.23 Aug '10 - 0:06 
Not having touched this article in a number of years, I can only assume it is some system problem with the code project site itself. I'm not sure I even have the source code any more! I will see what I can do.
If you have knowledge, let others light their candles at it.
Margaret Fuller (1810 - 1850)
[My Articles]  [My Website]

GeneralRe: Download Links seem deadadminChris Maunder16 Feb '11 - 1:40 
I found them and have restored them. Apologies for that.
cheers,
Chris Maunder
 
The Code Project | Co-founder
Microsoft C++ MVP

GeneralRe: Download Links seem deadmemberThe Man from U.N.C.L.E.16 Feb '11 - 1:41 
Thanks Chris
If you have knowledge, let others light their candles at it.
Margaret Fuller (1810 - 1850)
[My Articles]  [My Website]

GeneralTransparent controlsmemberjdkl5415 Jun '10 - 2:08 
Transparent controls revert to the color of the parent of your panel, instead of the panel color. Any workaround ?
GeneralIncorrect redrawing when scrollingmembercrypto_rsa28 May '10 - 1:25 
Hi, thanks for your nice demo. However, I tested the panels with Autoscroll property set to true and placing a large control inside the panel (so that the scrollbars are visible). When scrolling, the borders of the panel are not redrawn correctly. Also the border is drawn just around the client area, not the whole control (including the scrollbars). Any idea how to fix it?

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 15 Sep 2004
Article Copyright 2004 by The Man from U.N.C.L.E.
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid