c# - How to get all text from RichTextBox in WPF -
i need text wpf richtextbox.
everyone (how wpf rich textbox string, richtextbox (wpf) not have string property "text", http://msdn.microsoft.com/en-us/library/ms754041(v=vs.110).aspx , many more) agrees code one:
public static string gettext(richtextbox rtb) { textrange textrange = new textrange(rtb.document.contentstart, rtb.document.contentend); string text = textrange.text; return text; }
but add trailling "\r\n". in simple case:
<richtextbox x:name="mrichtextbox"> <flowdocument> <section> <paragraph> <run text="the dog brown. cat black."/> </paragraph> </section> </flowdocument> </richtextbox>
i get:
"the dog brown. cat black.\r\n"
it can seen minor issue need exact text.
- do know why?
- is safe ignore 1 trailing "\r\n"?
- is code correct?
- is there other gotcha (i mean differences between real text , gettext)?
thanks.
if don't need multiline can disable , shouldn't have problem.
you remove them this:
string text = textrange.text.replace(environment.newline, "");
also if want remove last ocurrence can use function 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 this.
string text = replacelastoccurrence(textrange.text,environment.newline,"");
every time have new line @ end, make sure handle acordingly.
Comments
Post a Comment