views:

1016

answers:

2

How do I get the text in RTF of a RichTextBox? I'm trying to get like this, but the property does not exists.

RichTextBox rtb = new RichTextBox();
string s = rtb.Rtf;

This does not work :S

+4  A: 

To get the actual XAML created by the user inside of the RichTextBox:

   TextRange tr = new TextRange(myRichTextBox.Document.ContentStart,
                                myRichTextBox.Document.ContentEnd);
   MemoryStream ms = new MemoryStream();
   tr.Save(ms, DataFormats.Xaml);
   string xamlText = ASCIIEncoding.Default.GetString(ms.ToArray());

EDIT: I don't have code in front of me to test, but an instance of the TextRange type has a Save (to stream) method that takes a DataFormats parameter, which can be DataFormats.Rtf

Mitch Wheat
DataFormats contains a Rtf member. Perhaps this method will work.
Tyalis
And now, how do I can set the RTF text of a RichTextBox?
rpf
ask another question! ;)
Mitch Wheat
+1  A: 

There are 2 RichTextBox classes, one from the winforms framework and one from the WPF framework:

System.Windows.Controls.RichTextBox wpfBox;
System.Windows.Forms.RichTextBox winformsBox;

Only the Winforms RichTextBox has an Rtf property, the other has a Document property which contains a FlowDocument.

Wouter
Yes I know...From the Document property, how do I get the RTF text?
rpf
You should look at converting a FlowDocument to an RTF string because the contents of the WPF control is a FlowDocument and not RTF. I have no idea how to accomplish this though.
Wouter