How to create a custom label control?

Here is the issue that I am having. I have a program that allows users to control the look and feel of the various controls on the forms, i.e. BoldText, Italics, Back/Fore Colors, etc.

Some of the functionality uses Label Controls as a "System Message" (usually large fonts and a distinct background color). My problem is that if a user decides to change all the Label controls to be Bold or Italics, this effects the "System Message" Bold and/or Italics properties, which brings me to creating a custom Label control. I've been playing around with the creation of a "UserControl" but I've never created one before so I'm not sure how to create a New Label control that contains FontSize, Bold, etc properties.
Who is Participating?
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
Your Label does not have to be in a control library (dll). If you use it only in 1 or 2 applications, create it in the application project, so it will be part of the .exe. It will be easier than having to carry and maintain proper version numbers in a dll.

You class could simply looks something like the following:
Public Class SystemMessage
     Inherits System.Windows.Forms.Label

     Public Sub New()

          MyBase.New() 'This calls the constructor for the basic Label over which you are building your own

          'If you want to keep the defaults but prevent the user from changing them from the Control Panel, set them here. This will override any change made by the user.
          Me.Font = New Font("Arial", 18, FontStyle.Bold Or FontStyle.Italic)
          Me.ForeColor = Color.Aquamarine
          Me.BackColor = Color.LightYellow
          'And so on ...

     End Sub

End Class

Open in new window

You need to compile the application at least once, and the new Control will appear at the in a new group bearing the name of your project at top of the Toolbox and will be ready to use. You might need to recompile if you make changes later.

This is basically a Label, so you use it just as you do with a standard label.

But since you change some of the properties in the constructor after calling MyBase.New, you override the defaults, and the properties that you define thus become the new defaults and do not follow the values set in the Control Panel.

Note that you can still change these same properties in the Properties window if you have different needs in some forms.
Bob LearnedCommented:
What type of application are you working with (WPF, Silverlight, ASP.NET web site, ...)?
BlakeMcKennaAuthor Commented:
Just a winforms app!
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Bob LearnedCommented:
With Windows Forms, if you create a control library project, and add a custom class, the control will inherit the Font property.

Here is some good reading material on custom controls in Windows Forms.

How To Create A Custom Button Control In VB.NET
BlakeMcKennaAuthor Commented:
I tried adding a "UserControl" to my app. I rebuilt the solution but I don't see the new control in the ToolBox anywhere...
Jacques Bourgeois (James Burger)PresidentCommented:
It usually shows in a tab with the name of your project, at the top of the Toolbox.

And if you did it with code similar to mine, it's not a UserControl, but an inherited control (some say a custom control).

A UserControl is interesting when you need a group of controls working together in a specific way. It's not really a control, its more like a small form that you can insert into another form.

An inhnerited control is better for what you want to do because it is a real control, is easier to manipulate through code, and offers more possibilities once you've learned all the tricks that you can do with inheritance.
BlakeMcKennaAuthor Commented:
I see what your saying James. I'll give this a try. It definitely makes more sense. Not sure where I was trying to go but thanks for the help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.