Hello and welcome to my tutorial for creating custom controls. If this tutorial gets enough positive feedback I will create more controls of this nature with tutorials to accompany them. Please bear in mind that there is no accompanying code or project file. If you wish to use this control you will have to write it. I assume readers of this are familiar with creating projects and basic class usage.

One of the things I have noticed is the lack of information on custom controls out there. Custom controls are extremely important in terms of code reuse and help to keep form code clean of so called "custom designer" code. For instance I tend to use roll over labels a lot in my code. The are a great piece of eye candy that can be used for descriptions and the like. When a user's mouse enters the label boundary the mouse changes to a different colour that the base fore colour. When the mouse leaves the label boundary the colour returns to the original base colour.

Setting Up The Project
First we need to set up the project. Select control library (user control) as the project type. We select this type rather that a class library because it sets up the references we need for the project. Name the project CustomControls. The solution explorer will contain a user control (UserControl1.vb) go ahead and delete that we wont be using it. In the solution explorer, right click the project name (CustomControls) and add a new class. Rename this class RollOverLabel.vb. If everything is correct you should have the following code when you click on the class.

VB Code:
  1. Public Class RollOverLabel
  3. End Class

Inheriting Functionality
As the dot net framework already provides a perfectly good label it makes sense not to rewrite the wheel. One of the pillars of object orientated programming is the ability to inherit functionality from existing classes. To inherit from the label class enter the following code directly under the RollOverLabel Class name.

VB Code:
  1. Inherits System.Windows.Forms.Label

Extending Functionality
Obviously having a new label class that has the exact same functionality as the original is not going to be sufficient enough. The first thing we are going to do is create a variable to hold the roll over color as the original label has no such storage. Update your class so it looks like this

VB Code:
  1. Public Class RollOverLabel
  2.     Inherits System.Windows.Forms.Label
  4.     Private HotColor As Color
  6. End Class

The variable we have created is of the type colour because we are dealing with colours after all. The easiest way of changing a labels colour to the roll over colour is to assign HotColor to the label's ForeColor property. The problem with doing this is we discard the original ForeColor value which we need to revert back to when the user's mouse leaves the labels boundary. To this end we need another variable to store the base colour. Update your code like so.

VB Code:
  1. Public Class RollOverLabel
  2.     Inherits System.Windows.Forms.Label
  4.     Private HotColor As Color
  5.     Private BaseColor As Color
  7. End Class

The next logical step is to allow the user of this control supply the roll over color. This is done via a standard property called RollOverColor(). Place the following code after the variable declarations.

VB Code:
  1. Public Property RollOverColor() As Color
  2.         Get
  3.             Return HotColor
  4.         End Get
  6.         Set(ByVal Value As Color)
  7.             HotColor = Value
  8.         End Set
  9.     End Property

Overriding Functionality
The idea of label is to change colours when the mouse enters/leaves the label boundary. Most standard controls already have methods to handle mouse enter/leave functionality. OO programming allows us to extend functionality of existing methods by using the Overrides keyword. This means that we don't have to rewrite functionality that is already in place. If you type Overrides OnMouseEnter() and press the return key VB will insert to following code.

VB Code:
  1. Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
  2.         MyBase.OnMouseEnter(e)
  4.     End Sub

VB places the correct method signature in for you as well as this line MyBase.OnMouseEnter(e). This line insures that the method carries out its standard functionality first. You should always place your code AFTER this line. Where going to add two statements to this code. Which are.
VB Code:
  1. BaseColor = Me.ForeColor
  2.         Me.ForeColor = HotColor

The reason for the first line is to insure that the most current ForeColor value is stored. This means that even if the user changes the ForeColor at design or runtime we always have the most current value. The second line changes the actual ForeColor value to the hot colour (remember we are in the mouse enter method).

We need one more Overridden method. That is the OnMouseLeave() method. Based on the previous method it is simple to work out what this method will look like.

VB Code:
  1. Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
  2.         MyBase.OnMouseLeave(e)
  3.         Me.ForeColor = BaseColor
  4.     End Sub

As you can see we simply change the ForeColor value back to the base colour when the mouse leaves the label boundary.

Adding Polish
While we could leave it at that, there are a few more things we can add to make this label more polished. I'm talking about attributes. Attributes allow you to change certain design time elements so that a custom control acts like a standard control in the toolbox and properties windows. The first thing we need to do is add the following line above the class name

VB Code:
  1. Imports System.ComponentModel

This will allow use to use attributes with our control. The first change we will make is the toolbox icon. At the moment it is a gear. Since this is a label we can "steal" the label icon an use it instead. To do this we enter the following line directly above the class name with not space between like so.

VB Code:
  1. <ToolboxBitmap(GetType(Label))> _
  2. Public Class RollOverLabel

The reason for no space is this line should actually be on the same line as the class name but that looks ugly.

VB Code:
  1. <ToolboxBitmap(GetType(Label))>  Public Class RollOverLabel

So instead we put it above and use the line break character to break up the line. There are two final attributes we can use that go directly above the RollOverColor() property. These are.
VB Code:
  1. <Description("Sets the color the label will change to when moused over")> _
  2.     <Category("Appearance")> _
  3.     Public Property RollOverColor() As Color

The first attribute changes the discription in the properties window and the second changes the category in which it is placed. If you had any difficuty following along here is the code in full.

VB Code:
  1. Imports System.ComponentModel
  3. <ToolboxBitmap(GetType(Label))> _
  4. Public Class RollOverLabel
  5.     Inherits System.Windows.Forms.Label
  7.     Private BaseColor As Color
  8.     Private HotColor As Color
  10.     <Description("Sets the color the label will change to when moused over")> _
  11.     <Category("Appearance")> _
  12.     Public Property RollOverColor() As Color
  13.         Get
  14.             Return HotColor
  15.         End Get
  17.         Set(ByVal Value As Color)
  18.             HotColor = Value
  19.         End Set
  20.     End Property
  22.     Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
  23.         MyBase.OnMouseEnter(e)
  24.         BaseColor = Me.ForeColor
  25.         Me.ForeColor = HotColor
  26.     End Sub
  28.     Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
  29.         MyBase.OnMouseLeave(e)
  30.         Me.ForeColor = BaseColor
  31.     End Sub
  32. End Class

I hope you have enjoyed this project as much as I have enjoyed writing it. If you have any queries feel free to comment. Thanks

I have tested this code to verify functionality. The Icon will not change until you import as a .dll into another project. After you have built the full source code a .dll file will be in the applications bin/release folder for you to distribute. If you intend to use this ode on a commercial application please include the following.

' Created by Dean McDonnell 18/08/2008 '