Click here to Skip to main content
13,457,165 members
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 15 Feb 2007

Creating a Rich Label

, 15 Feb 2007
Rate this:
Please Sign up or sign in to vote.
Extending the Windows.Forms.Label control.

Sample image


I was looking for a control that I could use for different kinds of headings and captions. After creating several separate controls, I decided to create just one control: a control that could be configured to have different colors, different borders, and that would respond on a mouse-action.


The best way to start doing this is to look for a good base. Instead of creating a control from scratch, I decided to inherit from the existing Windows.Forms.Label. By doing this, I don't have to write any code to draw the text; the base control will handle this for me.


In addition to the existing properties of the Label control, I created the following properties:

  • BackColor1: This is not a new property. It is the existing BackColor property of the Label control I'm inheriting from. I renamed it to BackColor1 because there will follow a second BackColor.
  • BackColor2: By using two colors, I can paint the color with a nice gradient.
  • BackColorHover1: When entering the control with your mouse, you can choose which color will be used for the first gradient-color. If you don't want any action, use the same color as the BackColor1 property.
  • BackcolorHover2: Same as the one above.
  • BorderLeft, BorderTop, BorderBottom, BorderRight: The color of the borders of the control. Set the BorderStyle to None to get this effect. I've chosen this BorderStyle so you can still use the Fixed3D and the FixedSingle if you need to.
  • Shadow: Setting this to True will draw a small white line on the left and the top side of the control. I need this when I want to create an OutlookBar-like heading. Set to False if you don't like it.
  • GradientMode: Choose the gradient you want.
  • GradientModeHover: Maybe you want a different gradient when the mouse is on the control.


There's not a lot of action with this control: when entering or leaving the control with the mouse, it will use a private field IsHovering to store the control's state. The drawing happens in the Paint and the PaintBackGround methods, nothing difficult about this. Take a look at the code and you will find out that it is pretty simple.


When creating a ClassLibrary for this type of controls, don't forget to explicitly add a reference to System.Windows.Forms and System.Drawing, they are not included by standard! If you want to add designer-markups, use System.ComponentModel. I have used this class to rename the existing BackColor property to BackColor1, and to get my new properties in the Appearance category in the Property Designer.


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


About the Author

Star Vega
Software Developer
Belgium Belgium
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
bluealert45529-Sep-10 21:04
memberbluealert45529-Sep-10 21:04 
Thank you!
jase_02428-Apr-10 9:37
memberjase_02428-Apr-10 9:37 
Questionmulti font, multi colored Pin
TheCardinal20-May-08 10:43
memberTheCardinal20-May-08 10:43 
AnswerRe: multi font, multi colored Pin
kissdznuts21-Jul-08 7:51
memberkissdznuts21-Jul-08 7:51 
GeneralRe: multi font, multi colored Pin
TheCardinal21-Jul-08 18:13
memberTheCardinal21-Jul-08 18:13 
GeneralRe: multi font, multi colored Pin
Star Vega21-Jul-08 22:13
memberStar Vega21-Jul-08 22:13 
GeneralRe: multi font, multi colored Pin
TheCardinal21-Jul-08 22:26
memberTheCardinal21-Jul-08 22:26 
QuestionTo be used in commercial app? Pin
Joseph Wee6-Jan-08 19:10
memberJoseph Wee6-Jan-08 19:10 
GeneralThanks very much.... Pin
djhampson24-May-07 23:08
memberdjhampson24-May-07 23:08 
QuestionGreat control..... [modified] Pin
QWERTYtech3-Apr-07 8:26
memberQWERTYtech3-Apr-07 8:26 
GeneralSelectAll Pin
scalpa9818-Mar-07 0:52
memberscalpa9818-Mar-07 0:52 
QuestionHow easy is it to override controls!! Pin
graemea10021-Feb-07 11:33
membergraemea10021-Feb-07 11:33 
AnswerRe: How easy is it to override controls!! Pin
Jan Geerinckx22-Feb-07 7:27
memberJan Geerinckx22-Feb-07 7:27 
GeneralDude! You are a coding freak! ;-) [modified] Pin
BarryGSumpter15-Feb-07 14:16
memberBarryGSumpter15-Feb-07 14:16 
GeneralRe: Dude! You are a coding freak! ;-) Pin
AndrewVos20-Feb-07 22:40
memberAndrewVos20-Feb-07 22:40 
GeneralGood Job! Pin
RickDotNet15-Feb-07 12:08
memberRickDotNet15-Feb-07 12:08 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180322.1 | Last Updated 15 Feb 2007
Article Copyright 2007 by Star Vega
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid