How to convert RichTextBox Contents to a String

There is no direct method or property in RichTextBox that can extract the document or contents of a RichTextBox to a string. To convert the contents to a string, we first need to read the contents of a RichTextBox in a TextRange object and use TextRange.Text property to convert it to a string.

The following code snippet reads a RichTextBox contents from start to end and converts to a string.

string ConvertRichTextBoxContentsToString(RichTextBox rtb)

{

    TextRange textRange = new TextRange(rtb.Document.ContentStart,

        rtb.Document.ContentEnd);

    return textRange.Text;

}