RichTextLabel WinForms Control

The Solution

After deciding to use the RichTextBox , I've created an RTF file with the text for my nag dialog (I've used the good ol' WordPad to do this). I've added the RTF file to the component's project and marked it as an embedded resource:

Marking an RTF file as embedded resource

I've added code to load the RTF text into the RichTextBox.Rtf property from inside the nag dialog Load event handler, which was rather easy:

' Get a reference to this type's assembly.
Dim Assm As [Assembly] = Me.GetType().Assembly
' Open the required resource and load the RTF text from it.
Dim RtfStream As System.IO.Stream = Assm.GetManifestResourceStream("NagDialog.NagDialogText.rtf")
Me.RichTextBox1.LoadFile(RtfStream, RichTextBoxStreamType.RichText)
RtfStream.Close()

The stream returned from the Assembly.GetManifestResourceStream is simply passed to the RichTextBox.LoadFile method.

The Assembly.GetManifestResourceStream method requires a resource name in the form <Namespace>.<FileName>, where <Namespace> is the project's root namespace and <FileName> is the actual file name (as it appears in the Solution Explorer). Please bear in mind that the resource name is case sensitive.

I wanted the nag dialog to display the same product information that was already embedded into the assembly's manifest as attributes (i.e. AssemblyProductAttribute , AssemblyCompanyAttribute , etc.). In order to do that, I've used substitution strings inside the RTF text. After loading the text, I've replaced the substitution strings with the actual attribute values, for example:

Dim Attrs() As Object = Assm.GetCustomAttributes(GetType(AssemblyTitleAttribute), False)
If (Not Attrs Is Nothing) AndAlso (Attrs.Length > 0) Then
  Me.RichTextBox1.Rtf = Me.RichTextBox1.Rtf.Replace("[0]", DirectCast(Attrs(0), AssemblyTitleAttribute).Title)
End If

My first attempt was to use substitution strings with curly braces (e.g. "{0}"), suitable for passing to the String.Format method. However, I've quickly realized that this approach won't work with RTF-formatted text, because curly braces have special meaning in RTF (they're are used to enclose RTF groups). The only other "forbidden" character is the backslash "\" character, so anything else can be used.

The other thing to watch out when using string substitution in RTF is to make sure that the whole substitution string doesn't contain different formatting. Otherwise the string will get escaped in the RTF text and you won't be able to replace it. For example, if you enter "(0 ) " as a substitution string with the closing brace formatted as bold, you'll get "(0\b )" as a result.

Let's recap quickly what you have to do in order to use RTF in a Form using the described technique:

  1. Write the RTF text in your RTF editor of choice (WordPad works quite well; Word adds too much overhead). Don't forget to add substitution strings for the product information that will be loaded from the assembly.
  2. Add the RTF file to your project and mark it as embedded resource (set the Build Action property of the file to 'Embedded Resource').
  3. Place a RichTextBox control on your dialog and set its properties that will make it look like a label (colors, TabStop , etc.).
  4. Write code to load the RTF into the RichTextBox control and to do the substitutions along the way.

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.

“In order to understand recursion, one must first understand recursion.”