Click here to Skip to main content
15,073,154 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a richtextbox.
in this richtextbox i add some picture and text
when i use
richTextBox1.Text += " ";
For add space in richtextbox the picture is cleared.
and i don't want use of appendtext
Posted
Comments
   
Why on Earth you don't want append text?
Which RichTextBox? WPF or Forms? Tag it!
--SA
jiji2663 29-Jun-11 1:53am
   
in Form
jiji2663 29-Jun-11 1:58am
   
Why on Earth you don't want append text?
i every thime that add picture or text in a richtextbox i want add space
if i use of appedtext when want to add a text or picture between a text or picture the space append to end of richtextbox
tnx

1 solution

Adding + " " is doubly ineffective. First, strings are immutable, so + does not change a string, it creates a brand new string and copies all old content plus a new one in this new string. Secondly, reading and then writing back the property Text becomes dead slow when the text is long.

See more detailed explanation in my past answer: Difference Between Mutable , Immutable string in C#[^].

Your picture disappears because you assign text and loose formatting. This properties treats the content of the control as the plain text. You would need to operate the property Rtf instead, but it would be slow by the reason I already explained.

Correct answer is asking the length of the content from the control itself, selecting the text at the very end with selection ends equal to zero. Then you need to assign the "selected text" property. You may also want to remember old cursor position and restore it after appending.

Look, you failed to tag your question with your UI library and did not specify exact type of RichTextBox. You created a problem for me as writing the answer in the form of pseudo-code (see previous paragraph) is inconvenient. You also did not receive any code. Therefore, now try to write the code yourself. In don't want to write two version just because you did not ask the question properly. If you have any questions, I'll answer them.

Good luck,
—SA
   

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