Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load an RTF file from resources to a RichTextBox in a WPF application

I am trying to load the content of an RTF file, which I have put inside resources (through Project->Properties->Resources->Add File).

I want to load Agreement.rtf's content to a RichTextBox and I have tried the following:

Dim stream As Stream
stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(My.Resources.ResourceManager.GetObject("Agreement").GetType(), "IOpzioni.Agreement.rtf")
RichTextBox1.SelectAll()

RichTextBox1.Selection.Load(stream, DataFormats.Rtf)

also

   stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(My.Resources.Agreement.GetType(), "IOpzioni.Agreement.rtf")

IOpzioni is my default namespace (I have double checked that).

Nothing seems to work. What is the correct way to do this?

like image 580
user1035909 Avatar asked Dec 20 '11 12:12

user1035909


1 Answers

I achieved this in a somewhat simple way in my WPF application. See my blog here: http://devdare.blogspot.com/2014/03/wpf-loading-rtf-document-in-richtextbox.html

  1. In your WPF projects, add a Resources.resx file (if it's not already there)
  2. Add your RTFDoc.rtf in your Resources.resx file
  3. Along with your Resources.resx file, there would be code behind file: Resources.Designer.cs. Open it and copy its namespace and class name. In my case it is Surf.Resources.Resource1

I used this to load the RTF resource in my WPF RichTextBox control. Here are the lines from the code behind:

using Surf.Resources;

void Surface_Loaded(object sender, RoutedEventArgs e)
{             
    var stream = new MemoryStream(Encoding.Unicode.GetBytes(Resource1.RTFDoc));
    RichTextBox1.Selection.Load(stream, DataFormats.Rtf);
}

Surf is my project name here. Hope this helps.

like image 136
Farrukh Waheed Avatar answered Nov 09 '22 20:11

Farrukh Waheed