It's quite boring, error prone work, so I've encapsulated the technique into an easy-to-use control - the RichTextLabel
control:
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:
- Add the
RichTextLabel.vb
orRichTextLabel.cs
source file to you project and recompile. TheRichTextLabel
control is derived fromUserControl
, so it will be added to the Visual Studio .NET toolbox automatically. - 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 - 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:
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.
Comments