Click here to Skip to main content
15,747,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Group

how can i replace text in WPF RichTextBox with keep replaced text formatting??
Updated 28-Jun-16 21:24pm
BillWoodruff 30-Jun-16 0:39am    
It's important you make an attempt to code this yourself, and show your code here; include any error messages.

1 solution


           string[] textArray = RTB.SelectedText.Split(new char[] { '\n' });

           foreach (string strText in textArray)
               if (!string.IsNullOrEmpty(strText))
                   RTB.Rtf = RTB.Rtf.Replace(strText, strText.ToUpper());
Share this answer
BillWoodruff 29-Jun-16 14:23pm    
+4 I think you are on your way to an excellent answer here. I'd like to see you earn a #5 :) by handling the case where you replace ONLY within the selection. As it is now, you'd replace every occurrence of the matched-string in ALL the Rtf.

Also, let me ask you to think about why you chose to split the Selection in the first place. What if the user wanted to replace content that included line-breaks ?

fyi: you can use an "empty" char[] and the 'RemoveEmptyEntries option, and the resulting string[] will be only the Rtf content lines.

cheers, Bill

private char[] splitChars = new char[] {};

var splitstr = someString.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900