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

Customising the .NET Panel control

, 15 Sep 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on customising the .NET Panel control to give it a gradient fill, and round corners.

Sample Image - custompanel.png


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.


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.


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.


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
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 Pinmemberjokefan13-May-14 16:51 
BugScrolling problem when AutoScroll is true Pinmemberaletsan1-Sep-13 10:14 
QuestionEasiest custom panel PinmemberKartik Goyal18-May-13 23:38 
Questiongood PinmemberyannKemp13-Sep-12 3:29 
QuestionGreat Code PinmemberElizalde G. Baguinon22-Jul-12 18:42 
GeneralPlease reinstate your downloads for this tantalizing component! Pinmemberterryd25-Aug-10 11:23 
GeneralDownload Links seem dead Pinmemberbyytas13-Aug-10 2: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 dead PinmemberThe Man from U.N.C.L.E.23-Aug-10 1:06 
GeneralRe: Download Links seem dead PinadminChris Maunder16-Feb-11 2:40 
GeneralRe: Download Links seem dead PinmemberThe Man from U.N.C.L.E.16-Feb-11 2:41 
GeneralTransparent controls Pinmemberjdkl5415-Jun-10 3:08 
GeneralIncorrect redrawing when scrolling Pinmembercrypto_rsa28-May-10 2:25 
GeneralRe: Incorrect redrawing when scrolling PinmemberThe Man from U.N.C.L.E.28-May-10 2:40 
GeneralUnable to download code Pinmemberhswear38-Apr-10 16:33 
GeneralRe: Unable to download code Pinmemberhswear38-Apr-10 16:35 
QuestionError BC30002 - Type MyApp.CustomPanel is not defined PinmemberMember 31389385-Apr-10 11:09 
QuestionFlickering Issue PinmemberPareshM22-Jan-09 3:31 
AnswerRe: Flickering Issue PinmemberYann Romefort4-Mar-09 23:10 
GeneralMine works great! Thanks for the code! Pinmember234wer28-Oct-08 16:22 
GeneralC++ /CLI PinmemberXaria14-Oct-08 20:01 
AnswerRe: C++ /CLI PinmemberThe Man from U.N.C.L.E.14-Oct-08 23:46 
QuestionBlack edges Pinmemberlaith_b29-Apr-08 11:23 
QuestionProgrammatically created CustomPanel is not transparent PinmemberDustinDS24-Aug-07 6:16 
GeneralExcellent component! Pinmemberphilnad13-Jul-07 8:41 
QuestionBackground image? Pinmembersgmisguided14-Jun-07 20:25 
AnswerRe: Background image? Pinmemberca0v23-Oct-07 9:08 
Generalusing and import Pinmemberroel_v13-Mar-07 0:40 
GeneralCustom Panel having a User Control Pinmembersrinivas_vemla22-Nov-06 12:54 
Question1-pixel width missing from Top and Left.. [modified] Pinmemberskurs25-Oct-06 8:21 
AnswerRe: 1-pixel width missing from Top and Left.. Pinmemberboohooo9-Mar-09 1:58 
QuestionAdding the Control to the Toolbox in VB .NET Pinmemberskurs19-Oct-06 8:57 
AnswerRe: Adding the Control to the Toolbox in VB .NET Pinmemberskurs19-Oct-06 11:47 
AnswerRe: Adding the Control to the Toolbox in VB .NET PinmemberThe Man from U.N.C.L.E.20-Oct-06 0:02 
QuestionControls not visible at runtime PinmemberHakkoos12-Oct-06 1:03 
AnswerRe: Controls not visible at runtime PinmemberHakkoos23-Oct-06 6:33 
Generalhi Pinmembergaik1131-Aug-06 9:33 
GeneralSingle Pixel Border Width Pinmembermaurocam18-Aug-06 6:48 
GeneralBackColor Property Pinmembervaderjm3-Aug-06 16:45 
GeneralRe: BackColor Property [modified] Pinmembervaderjm3-Aug-06 17:42 
AnswerRe: BackColor Property PinmemberThe Man from U.N.C.L.E.4-Aug-06 5:18 
GeneralRe: BackColor Property [modified] Pinmembervaderjm5-Aug-06 6:34 
GeneralFixedSingle border Pinmemberzettinger22-Jun-06 0:48 
GeneralRe: FixedSingle border Fix PinmemberKevmill6-Aug-06 10:31 
GeneralRe: FixedSingle border Fix Pinmemberyarborg11-Jun-08 5:40 
QuestionUse of Control in Commercial Programs Pinmembervaderjm26-Apr-06 8:30 
AnswerRe: Use of Control in Commercial Programs PinmemberThe Man from U.N.C.L.E.26-Apr-06 8:38 
GeneralRe: Use of Control in Commercial Programs Pinmembervaderjm26-Apr-06 14:31 
GeneralRe: Use of Control in Commercial Programs PinmemberSimonS11-Jun-06 3:02 
AnswerRe: Use of Control in Commercial Programs PinmemberThe Man from U.N.C.L.E.12-Jun-06 4:51 
GeneralRe: Use of Control in Commercial Programs PinmemberSimonS12-Jun-06 6:47 

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 | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 15 Sep 2004
Article Copyright 2004 by The Man from U.N.C.L.E.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid