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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
What type of application are you working with (WPF, Silverlight, ASP.NET web site, ...)?
BlakeMcKennaAuthor Commented:
Just a winforms app!
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
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Jacques Bourgeois (James Burger)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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.