richtext.text property in wpf using C#.net [Resolved]

Posted by Sudhakar_A under WPF on 9/24/2013 | Points: 10 | Views : 3513 | Status : [Member] | Replies : 6
I wan to write an text to the richtextbox in wpf application.
But it is not giving richtextbox.Text property to display.
i.e richtextbox.text ="successfully send";


please help me sort out




Responses

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
WPF RichTextBox does not have any direct property or method that will convert its content to string which can in turn be used for manipulation. You could try something like this


TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);

string rtbContent = textRange.Text;

Now that you have the contents in a string, you can easily replace the part of the string you want.


C# String in RichTextBox Wpf
yourRichTextBox.AppendText("Successfully Sent...");


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Because System.Windows.Controls.RichTextBox does not have a property for Text to detect its value, you may detect its value using the following

string _Text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;


--May be this?
TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text = "Successfully Sent";


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
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;


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
References:
http://stackoverflow.com/questions/1367256/set-rtf-text-into-wpf-richtextbox-control
http://stackoverflow.com/questions/5715961/c-sharp-string-in-richtextbox-wpf

Extract Text from RichTextBox:
string StringFromRichTextBox(RichTextBox rtb)
{
TextRange textRange = new TextRange(
// TextPointer to the start of content in the RichTextBox.
rtb.Document.ContentStart,
// TextPointer to the end of content in the RichTextBox.
rtb.Document.ContentEnd
);

// The Text property on a TextRange object returns a string
// representing the plain text content of the TextRange.
return textRange.Text;
}


Reference:
http://stackoverflow.com/questions/15478570/string-with-xaml-or-rtf-to-c-sharp-wpf-richtextbox

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Simple and Exact solution is as follows:
To write text to a RichTextBox, you need to create a Flowdocument.
To do this from code, you would do this...

Paragraph para = new Paragraph();
para.Inlines.Add(new Run("Successfully Sent.."));
FlowDocument fd = new FlowDocument(para);
richTextBox1.Document = fd;



Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
--Alternate is:

private void Form_Load(object sender, EventArgs e)
{
richText.AppendText("Hello, World!");
}



Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response