-1

I need to get exactly all text from a WPF RichTextBox.

Everyone (How to get a WPF rich textbox into a string, RichTextBox (WPF) does not have string property "Text", http://msdn.microsoft.com/en-us/library/ms754041(v=vs.110).aspx and many more) agrees a code like this one:

public static string GetText(RichTextBox rtb)
{
    TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
    string text = textRange.Text;
    return text;
}

but it add a trailling "\r\n". In a very simple case:

<RichTextBox x:Name="mRichTextBox">
  <FlowDocument>
    <Section>
      <Paragraph>
        <Run Text="The dog is brown. The cat is black."/>
      </Paragraph>
    </Section>
  </FlowDocument>
</RichTextBox>

I get:

"The dog is brown. The cat is black.\r\n"

It can be seen as a minor issue but really I need exact text.

  • Do you know why?
  • Is it safe to simply ignore one trailing "\r\n"?
  • Is my code correct?
  • Is there any other gotcha (I mean differences between real text and what I get from my GetText)?

Thanks.

Community
  • 1
  • 1
MuiBienCarlota
  • 2,355
  • 1
  • 28
  • 32

2 Answers2

0

If you don't need the multiline you can disable it and shouldn't have this problem.

You could also remove them like this:

  string text = textRange.Text.Replace(Environment.NewLine, "");

Also if you only want to remove the last ocurrence you can use a function like this:

public static string ReplaceLastOccurrence(string Source, string Find, string Replace)
{
    int Place = Source.LastIndexOf(Find);
    string result = Source.Remove(Place, Find.Length).Insert(Place, Replace);
    return result;
}

And use it like this.

 string text = ReplaceLastOccurrence(textRange.Text,Environment.NewLine,"");

Every time you have a you will get a New Line at the end, so make sure you handle it acordingly.

Luis Tellez
  • 2,785
  • 1
  • 20
  • 28
  • I need multiline edit ("The dog is brown. The cat is black." is only a sample) and I need exact text. I resolved to remove a possible "\r\n" at the end of my string but I need to know why it appends and if there is other differences. – MuiBienCarlota Apr 04 '14 at 15:24
  • You have and my guess is that each time you do you have a new line if not empty. – Luis Tellez Apr 04 '14 at 17:53
  • And you should use the method with the Environment.NewLine and not with \r\n, its cleaner and portable. – Luis Tellez Apr 04 '14 at 18:00
  • I'm afraid you're right about one , one NewLine rule. Do you know if there is other differences between entered text and GetText? – MuiBienCarlota Apr 05 '14 at 17:09
  • 1
    No, im not sure if you could enter the text without paragraph to avoid this, anyway if you remove the Environment.NewLine for each Paragraph it should be OK. – Luis Tellez Apr 07 '14 at 18:39
  • I was searching a way to have same contents after roundtrip of SetText(GetText()). I will then add `if (text.EndsWith(Environment.NewLine)) text = text.Remove(text.Length - Environment.NewLine.Length);` to my GetText hoping no other differences exists. Thanks. – MuiBienCarlota Apr 08 '14 at 11:44
0

But you are getting the actual text.
You don't think a parargraph should end with a new line?

Try this and copy paste from the screen to notepad

<RichTextBox x:Name="mRichTextBox">
    <FlowDocument>
        <Section>
            <Paragraph/>
            <Paragraph/>
            <Paragraph>
                <Run Text="The dog is brown. The cat is black."/>
            </Paragraph>
        </Section>
    </FlowDocument>
</RichTextBox>
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • I have seen that each empty Paragraph is exported as a newline but I don't understand why the last non empty Paragraph is generating a trailing NewLine. In each text editor (notepad, Word, Visual Studio and any other), you have a difference between ending a text with or without a new line. If a newline is forced, I can remove it but I need to know if it will be the only difference. – MuiBienCarlota Apr 04 '14 at 15:32
  • And you have seen that a non empty Paragraph end with a new line. Are you starting to see a pattern here? – paparazzo Apr 04 '14 at 15:49
  • I need to be able to have exactly same content after Setting text gathered by a GetText. I use following SetText: `public static void SetText(RichTextBox rtb, string text) { TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd); textRange.Text = text; }` If I don't remove one trailing "\r\n", I have a new empty Paragraph after each SetText(GetText()) wich is troublesome. But I also need to know if there is other differences between entered text and what I get. – MuiBienCarlota Apr 05 '14 at 17:26