Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 42,585 times

Contents

Downloads

Related Categories

RichTextLabel WinForms Control - The Solution

palom

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.

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 .NET framework, publisher of the www.VBInfoZine.com ezine and the author of the Dynamic AutoComplete Tool .NET component (dact.lamarvin.com).

Comments

  • Re: [4584] RichTextLabel WinForms Control

    Posted by dgauerke on 26 Jul 2006

    I moved the control into a seperate dll (along with the editor), but the editor no longer finds the embedded rtf files in the exe.  When I move the control back into the main application it wo...

  • doubt

    Posted by sherry on 22 Nov 2004

    i have a doubt...
    when we use an input box in vb.net on clicking the ok button i get the input into the text box where required but how to activate the cancel button???