RichTextLabel WinForms Control

Page 3 of 3
  1. Introduction
  2. The Solution
  3. Wrapping it up in a Control

Wrapping it up in a Control

It's quite boring, error prone work, so I've encapsulated the technique into an easy-to-use control - the RichTextLabel control:

The RichTextLabel control at runtime

The control is intended for reuse in source form . The accompanying solution contains both, the VB.NET version as well as the C# version of the control. The RichTextLabel control exposes just one, all important RtfResourceName property. You'll need to set the property to the name of the RTF text resource you wish to display in the control. Everything else is done automatically.

In order to use the RichTextLabel control within your project, you'll have to follow these steps:

  1. Add the RichTextLabel.vb or RichTextLabel.cs source file to you project and recompile. The RichTextLabel control is derived from UserControl , so it will be added to the Visual Studio .NET toolbox automatically.
  2. Add an RTF file to your project and set its build action to 'Embedded Resource'. You can have the following substitution strings in the file:

    Substitution String Replaced by this property's value
    [Title] AssemblyTitleAttribute.Title
    [Product] AssemblyProductAttribute.Product
    [Copyright] AssemblyCopyrightAttribute.Copyright
    [Company] AssemblyCompanyAttribute.Company
  3. Place the RichTextLabel control onto a Form and set its RtfResourceName property to the name of the RTF file you've added in step 2.

In order to make things easier for you, the RtfResourceName property has an associated type editor ( RtfResourceNameEditor ), which displays a popup list of all the RTF resource files available in the current assembly:

The RichTextLabel control at design-time

In order to keep the RichTextLabel control as small as possible, I've put the type editor in a separate LaMarvin.Windows.Forms.RichTextLabel.Design.dll assembly that needn't be directly referenced by the project containing the RichTextLabel control. If you want to take advantage of the richer design-time support, just copy the LaMarvin.Windows.Forms.RichTextLabel.Design.dll assembly to your project's output directory.

If you'd like to edit the RTF files added to your project comfortably from within the Visual Studio .NET IDE, you might want to associate the .RTF file extension with WordPad. Right-click on any RTF file in the Solution Explorer and select the "Open With..." command. In the Open With dialog box, add WordPad.exe to the list of available programs and set it as the default editor for .RTF files. After that, double-clicking an RTF file in the Solution Explorer will open the file in WordPad automatically.

You might also like...

Comments

About the author

Palo Mraz

Palo Mraz United States

I live in Slovakia with my wife, two sons (fulltime), one daughter (occasionally) and a dog. I've been doing Microsoft Windows development since 1988; primarily in VB. I'm a big fan of the MS .N...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”