Serialcoder en Français Serialcoder in English
TEL : +33 (0)9 72 13 15 17

Windows Forms FAQ resources

22. Windows Forms RichTextBox

22.7 How can I load an embedded rich text file into a richtextbox?


You use the LoadFile method of the RichTextBox, passing it a streamreader based on the resource. So include your RTF file as an embedded resource in your project, say RTFText.rtf. Then load your richtextbox with code such as

     Stream stream = this.GetType().Assembly.GetManifestResourceStream("MyNameSpace.RTFText.rtf");
     if (stream != null)
     {
          StreamReader sr = new StreamReader(stream);
          richTextBox1.LoadFile(stream, RichTextBoxStreamType.RichText);
          sr.Close();
     }

Here is a VB snippet. Depending on your default namespace settings, you may not need explicit reference to the namespace in the GetManifestResourceStream argument.

     Dim stream As Stream = Me.GetType().Assembly.GetManifestResourceStream("MyNameSpace.RTFText.rtf")
     If Not (stream Is Nothing) Then
          Dim sr As New StreamReader(stream)
          richTextBox1.LoadFile(stream, RichTextBoxStreamType.RichText)
          sr.Close()
     End If