I have code that has chemical compounds that have small font for the subscript. I currently have this code that transfers it from one RichTextBox
to another one on a Button click.
myRichTextBox.Text += otherRichTextBox.Text
In otherRichTextBox
I have the compound with varying font sizes however when I do this I end up with a string in myRichTextBox
that doesn't keep the varying font sizes and sets them all to the boxes main properties font and size.
To copy text including formatting you should use the usual RTB way:
This is the way to go, no matter what you do:
SelectionFont
,SelectionColor
,SelectionAlignment
etc..Cut
,Copy
orPaste
Find
text orAppendText
Here is how to do what you asked about:
To insert the portion of text at position
n
you writeYou need to go by the rule anytime you want to change formatted text in pretty much any way!
A little bit involved but guaranteed to work correctly as it goes by the book.
To simply 'clone' the full text, go by Grant's code:
It is possible to work with the raw
Rtf
codes, if you know what you are doing, but even if some things may still look ok for a while because some errors and most redundancies get ignored, it has a tendency to collect crap.. So you should follow the golden rule:Update: Here is a nice way to solve your problem correctly with only two lines! (But you still need to live by the rule..)
From the documentation on msdn:
So to assign the value, with formatting, use this:
I've replaced
+=
with=
because I'm not sure you meant to append the value, rather than just replace it. If you do use+=
, you may run into issues due to the "rtf" codes being appended one after the other. However, give it a try... you may not run into any issues at all.