We help IT Professionals succeed at work.

Copy from one RichTextBox to an other

andre72
andre72 used Ask the Experts™
on
Hi,

I need to copy formated text from one RichText to an other.
These steps are needed:
RichTextBox_Source: Select all (Copy)
RichTextBox_Target: Find value (eg. XY) and replace by content from RichTextBox_Source

What is the solution for?

Thanks

Andre
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Something like this:
textBoxSource.SelectionStart = 0;  
textBoxSource.SelectionLength = textBoxSource.TextLength;
string TargetOld = textBoxTarget.Text
string TargetNew=TargetOld.Replace("XY", textBoxSource.SelectedText)
textBoxTarget.Text=TargetNew
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
That'll lose all the formatting because you're setting the .Text property.

Try something more like:
string ReplaceThis = "XY";
            int position = RichTextBox_Target.Find(ReplaceThis);
            if (position != -1)
            {
                RichTextBox_Target.Select(position, ReplaceThis.Length);
                RichTextBox_Target.SelectedRtf = RichTextBox_Source.Rtf;
            }

Open in new window

Author

Commented:
Thanks for your support