Click here to Skip to main content
15,879,095 members
Articles / Programming Languages / Visual Basic
Article

Creating a Rich Label

Rate me:
Please Sign up or sign in to vote.
4.40/5 (10 votes)
15 Feb 2007CPOL2 min read 57.8K   725   35   16
Extending the Windows.Forms.Label control.

Sample image

Introduction

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.

Structure

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.

Properties

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.

Methods

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.

Remarks

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.

License

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


Written By
Software Developer
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
bluealert45529-Sep-10 21:04
bluealert45529-Sep-10 21:04 
GeneralPURE EXCELLENCE... Pin
jase_02428-Apr-10 9:37
jase_02428-Apr-10 9:37 
Questionmulti font, multi colored Pin
TheCardinal20-May-08 10:43
TheCardinal20-May-08 10:43 
AnswerRe: multi font, multi colored Pin
Jon_Boy21-Jul-08 7:51
Jon_Boy21-Jul-08 7:51 
GeneralRe: multi font, multi colored Pin
TheCardinal21-Jul-08 18:13
TheCardinal21-Jul-08 18:13 
GeneralRe: multi font, multi colored Pin
Star Vega21-Jul-08 22:13
Star Vega21-Jul-08 22:13 
GeneralRe: multi font, multi colored Pin
TheCardinal21-Jul-08 22:26
TheCardinal21-Jul-08 22:26 
QuestionTo be used in commercial app? Pin
Joseph Wee6-Jan-08 19:10
Joseph Wee6-Jan-08 19:10 
GeneralThanks very much.... Pin
djhampson24-May-07 23:08
djhampson24-May-07 23:08 
QuestionGreat control..... [modified] Pin
QWERTYtech3-Apr-07 8:26
QWERTYtech3-Apr-07 8:26 
GeneralSelectAll Pin
scalpa9818-Mar-07 0:52
scalpa9818-Mar-07 0:52 
QuestionHow easy is it to override controls!! Pin
graemea10021-Feb-07 11:33
graemea10021-Feb-07 11:33 
AnswerRe: How easy is it to override controls!! Pin
Star Vega22-Feb-07 7:27
Star Vega22-Feb-07 7:27 
GeneralDude! You are a coding freak! ;-) [modified] Pin
BarryGSumpter15-Feb-07 14:16
BarryGSumpter15-Feb-07 14:16 
GeneralRe: Dude! You are a coding freak! ;-) Pin
AndrewVos20-Feb-07 22:40
AndrewVos20-Feb-07 22:40 
GeneralGood Job! Pin
RickDotNet15-Feb-07 12:08
RickDotNet15-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.