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 ?

209482 次浏览

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.

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

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.

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;
string GetString(RichTextBox rtb)
{
var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
return textRange.Text;
}

"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

How about just doing the following:

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

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

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

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