Click here to Skip to main content
15,867,704 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 391.8K   10.2K   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

 
AnswerRe: 1-pixel width missing from Top and Left.. Pin
boohooo9-Mar-09 0:58
boohooo9-Mar-09 0:58 
QuestionAdding the Control to the Toolbox in VB .NET Pin
skurs19-Oct-06 7:57
skurs19-Oct-06 7:57 
AnswerRe: Adding the Control to the Toolbox in VB .NET Pin
skurs19-Oct-06 10:47
skurs19-Oct-06 10:47 
AnswerRe: Adding the Control to the Toolbox in VB .NET Pin
The Man from U.N.C.L.E.19-Oct-06 23:02
The Man from U.N.C.L.E.19-Oct-06 23:02 
QuestionControls not visible at runtime Pin
Hakkoos12-Oct-06 0:03
Hakkoos12-Oct-06 0:03 
AnswerRe: Controls not visible at runtime Pin
Hakkoos23-Oct-06 5:33
Hakkoos23-Oct-06 5:33 
Generalhi Pin
gaik1131-Aug-06 8:33
gaik1131-Aug-06 8:33 
GeneralSingle Pixel Border Width Pin
maurocam18-Aug-06 5:48
maurocam18-Aug-06 5:48 
GeneralBackColor Property Pin
vaderjm3-Aug-06 15:45
vaderjm3-Aug-06 15:45 
GeneralRe: BackColor Property [modified] Pin
vaderjm3-Aug-06 16:42
vaderjm3-Aug-06 16:42 
AnswerRe: BackColor Property Pin
The Man from U.N.C.L.E.4-Aug-06 4:18
The Man from U.N.C.L.E.4-Aug-06 4:18 
GeneralRe: BackColor Property [modified] Pin
vaderjm5-Aug-06 5:34
vaderjm5-Aug-06 5:34 
GeneralFixedSingle border Pin
zettinger21-Jun-06 23:48
zettinger21-Jun-06 23:48 
GeneralRe: FixedSingle border Fix Pin
Kevmill6-Aug-06 9:31
Kevmill6-Aug-06 9:31 
GeneralRe: FixedSingle border Fix Pin
yarborg11-Jun-08 4:40
yarborg11-Jun-08 4:40 
QuestionUse of Control in Commercial Programs Pin
vaderjm26-Apr-06 7:30
vaderjm26-Apr-06 7:30 
AnswerRe: Use of Control in Commercial Programs Pin
The Man from U.N.C.L.E.26-Apr-06 7:38
The Man from U.N.C.L.E.26-Apr-06 7:38 
GeneralRe: Use of Control in Commercial Programs Pin
vaderjm26-Apr-06 13:31
vaderjm26-Apr-06 13:31 
GeneralRe: Use of Control in Commercial Programs Pin
SimonS11-Jun-06 2:02
SimonS11-Jun-06 2:02 
AnswerRe: Use of Control in Commercial Programs Pin
The Man from U.N.C.L.E.12-Jun-06 3:51
The Man from U.N.C.L.E.12-Jun-06 3:51 
GeneralRe: Use of Control in Commercial Programs Pin
SimonS12-Jun-06 5:47
SimonS12-Jun-06 5:47 
GeneralRe: Use of Control in Commercial Programs Pin
vaderjm14-Apr-07 7:23
vaderjm14-Apr-07 7:23 
QuestionProblem with background if panel is scrollable Pin
Borex4-Mar-06 4:15
Borex4-Mar-06 4:15 
AnswerRe: Problem with background if panel is scrollable Pin
Gary Noble16-Mar-06 10:51
Gary Noble16-Mar-06 10:51 
AnswerRe: Problem with background if panel is scrollable Pin
Borex16-Mar-06 11:49
Borex16-Mar-06 11:49 

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.