126

I am trying to set/get the text of my RichTextBox, but Text is not among list of its properties when I want to get test.Text...

I am using code behind in C# (.net framework 3.5 SP1)

RichTextBox test = new RichTextBox();

cannot have test.Text(?)

Do you know how come it can be possible ?

Nasreddine
  • 36,610
  • 17
  • 75
  • 94

11 Answers11

139

to set RichTextBox text:

richTextBox1.Document.Blocks.Clear();
richTextBox1.Document.Blocks.Add(new Paragraph(new Run("Text")));

to get RichTextBox text:

string richText = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
sma6871
  • 3,198
  • 3
  • 38
  • 52
66

There was a confusion between RichTextBox in System.Windows.Forms and in System.Windows.Control

I am using the one in the Control as I am using WPF. In there, there is no Text property, and in order to get a text, I should have used this line:

string myText = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.Document.ContentEnd).Text; 

thanks

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
39

The WPF RichTextBox has a Document property for setting the content a la MSDN:

// Create a FlowDocument to contain content for the RichTextBox.
        FlowDocument myFlowDoc = new FlowDocument();

        // Add paragraphs to the FlowDocument.
        myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
        myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
        myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
        RichTextBox myRichTextBox = new RichTextBox();

        // Add initial content to the RichTextBox.
        myRichTextBox.Document = myFlowDoc;

You can just use the AppendText method though if that's all you're after.

Hope that helps.

EightyOne Unite
  • 11,665
  • 14
  • 79
  • 105
18

Using two extension methods, this becomes very easy:

public static class Ext
{
    public static void SetText(this RichTextBox richTextBox, string text)
    {
        richTextBox.Document.Blocks.Clear();
        richTextBox.Document.Blocks.Add(new Paragraph(new Run(text)));
    }

    public static string GetText(this RichTextBox richTextBox)
    {
        return new TextRange(richTextBox.Document.ContentStart,
            richTextBox.Document.ContentEnd).Text;
    }
}
Smile4ever
  • 3,491
  • 2
  • 26
  • 34
16

There is no Text property in the WPF RichTextBox control. Here is one way to get all of the text out:

TextRange range = new TextRange(myRTB.Document.ContentStart, myRTB.Document.ContentEnd);

string allText = range.Text;
Chris Amelinckx
  • 4,334
  • 2
  • 23
  • 21
13
string GetString(RichTextBox rtb)
{
    var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
    return textRange.Text;
}
Marijn
  • 10,367
  • 5
  • 59
  • 80
user1143839
  • 131
  • 1
  • 2
10
RichTextBox rtf = new RichTextBox();
System.IO.MemoryStream stream = new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes(yourText));

rtf.Selection.Load(stream, DataFormats.Rtf);

OR

rtf.Selection.Text = yourText;
Vincenzo Costa
  • 930
  • 11
  • 17
8

How about just doing the following:

_richTextBox.SelectAll();
string myText = _richTextBox.Selection.Text;
Curtis
  • 5,794
  • 8
  • 50
  • 77
  • 1
    Best answer that i could find so far :) Here my code if you want to paste the Length in another Textbox in a GUI: `rtxb_input.SelectAll();` `txb_InputLength.Text = rtxb_input.Selection.Text.Length.ToString();` – Marty_in_a_Box Apr 24 '16 at 22:14
4

"Extended WPF Toolkit" now provides a richtextbox with the Text property.

You can get or set the text in different formats (XAML, RTF and plaintext).

Here is the link: Extended WPF Toolkit RichTextBox

Beytan Kurt
  • 2,203
  • 4
  • 34
  • 57
GiangLP
  • 79
  • 1
  • 3
0

To my big surprise the RichtTextBox does not return the same value as was set !

Setting a string With:

SelectAll()
RichTextBox.Selection.Text = "AA"

And returning with:

SelectAll()
Return RichTextBox.Selection.Text

Returns "AA" with carriage-return

Also using:

Dim Selection = New TextRange(rtbRichTextBox.Document.ContentStart, rtbRichTextBox.Document.ContentEnd)
Selection.Text = "AA"

And returning with:

Dim Selection = New TextRange(rtbRichTextBox.Document.ContentStart, rtbRichTextBox.Document.ContentEnd)
Return Selection.Text

Does the same: "AA" with carriage-return

The RichTextBox does not return the value as set Very incorrect behaviour !!

Is solved (circumvented) this by:

Dim Selection = New TextRange(rtbRichTextBox.Document.ContentStart, rtbRichTextBox.Document.ContentEnd.GetPositionAtOffset(-1))
Return Selection.Text
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Hub
  • 1
  • 1
-12

According to this it does have a Text property

http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox_members.aspx

You can also try the "Lines" property if you want the text broken up as lines.

Jaime Garcia
  • 6,744
  • 7
  • 49
  • 61