Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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)

Share

About the Author

Star Vega
Software Developer
Belgium Belgium
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberbluealert45529-Sep-10 21:04 
GeneralPURE EXCELLENCE... Pinmemberjase_02428-Apr-10 9:37 
Questionmulti font, multi colored PinmemberTheCardinal20-May-08 10:43 
AnswerRe: multi font, multi colored Pinmemberkissdznuts21-Jul-08 7:51 
GeneralRe: multi font, multi colored PinmemberTheCardinal21-Jul-08 18:13 
nope not yet Smile | :) still looking for a descent solution to this problem.
 
if youll find one let me know Smile | :)
 
"for true knowledge only comes thru understanding ones own questions ... if you dont do that then all you want is someone to do your homework for you and you will learn nothing (and go on to become a vb programmer and suffer the scorn and ridicule of us for all eternity etc etc)" - l a u r e n
 
"You can't have great software without a great team, and most software teams behave like dysfunctional families." - Jim McCarthy
 
TheCardinal
CTC-RDG

GeneralRe: multi font, multi colored PinmemberStar Vega21-Jul-08 22:13 
GeneralRe: multi font, multi colored PinmemberTheCardinal21-Jul-08 22:26 
QuestionTo be used in commercial app? PinmemberJoseph Wee6-Jan-08 19:10 
GeneralThanks very much.... Pinmemberdjhampson24-May-07 23:08 
QuestionGreat control..... [modified] PinmemberQWERTYtech3-Apr-07 8:26 
GeneralSelectAll Pinmemberscalpa9818-Mar-07 0:52 
QuestionHow easy is it to override controls!! Pinmembergraemea10021-Feb-07 11:33 
AnswerRe: How easy is it to override controls!! PinmemberJan Geerinckx22-Feb-07 7:27 
GeneralDude! You are a coding freak! ;-) [modified] PinmemberBarryGSumpter15-Feb-07 14:16 
GeneralRe: Dude! You are a coding freak! ;-) PinmemberAndrewVos20-Feb-07 22:40 
GeneralGood Job! PinmemberRickDotNet15-Feb-07 12:08 

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 | Mobile
Web04 | 2.8.140926.1 | Last Updated 15 Feb 2007
Article Copyright 2007 by Star Vega
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid