paradisonoir
paradisonoir

Reputation:

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

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 ?

Upvotes: 127

Views: 221169

Answers (11)

Hub
Hub

Reputation: 1

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

Upvotes: 0

Chris Amelinckx
Chris Amelinckx

Reputation: 4482

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;

Upvotes: 16

Smile4ever
Smile4ever

Reputation: 3704

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

Upvotes: 18

Vincenzo Costa
Vincenzo Costa

Reputation: 960

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;

Upvotes: 11

sma6871
sma6871

Reputation: 3298

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;

Upvotes: 139

Curtis
Curtis

Reputation: 5882

How about just doing the following:

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

Upvotes: 8

GiangLP
GiangLP

Reputation: 89

"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

Upvotes: 4

paradisonoir
paradisonoir

Reputation:

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

Upvotes: 67

Aleksandr Shin
Aleksandr Shin

Reputation: 131

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

Upvotes: 13

EightyOne Unite
EightyOne Unite

Reputation: 11795

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.

Upvotes: 39

Jaime Garcia
Jaime Garcia

Reputation: 7096

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.

Upvotes: -12

Related Questions