RichTextBox (WPF) does not have string property "Text"

C#WpfWpf ControlsRichtextbox

C# Problem Overview


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 ?

C# Solutions


Solution 1 - C#

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;

Solution 2 - C#

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

Solution 3 - C#

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.

Solution 4 - C#

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;
    }
}

Solution 5 - C#

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;

Solution 6 - C#

string GetString(RichTextBox rtb)
{
    var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
    return textRange.Text;
}

Solution 7 - C#

How about just doing the following:

_richTextBox.SelectAll();
string myText = _richTextBox.Selection.Text;

Solution 8 - C#

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;

Solution 9 - C#

"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

Solution 10 - C#

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

Solution 11 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionparadisonoirView Question on Stackoverflow
Solution 1 - C#sma6871View Answer on Stackoverflow
Solution 2 - C#paradisonoirView Answer on Stackoverflow
Solution 3 - C#EightyOne UniteView Answer on Stackoverflow
Solution 4 - C#Smile4everView Answer on Stackoverflow
Solution 5 - C#Chris AmelinckxView Answer on Stackoverflow
Solution 6 - C#user1143839View Answer on Stackoverflow
Solution 7 - C#CurtisView Answer on Stackoverflow
Solution 8 - C#Vincenzo CostaView Answer on Stackoverflow
Solution 9 - C#GiangLPView Answer on Stackoverflow
Solution 10 - C#HubView Answer on Stackoverflow
Solution 11 - C#Jaime GarciaView Answer on Stackoverflow