Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to add text to rich text box in c# without canceling all the formatting of the text.
because, .text functions returns new string that contain only text.

What I have tried:

all the .text functions and google
Updated 5-Feb-23 11:18am
Member 15627495 5-Feb-23 7:50am    
use concatenation of String :

RichTextBox.text += text_to_add ;

look at :
Dave Kreskowiak 5-Feb-23 9:32am    
You didn't read the question, did you? Appending to the Text property does not work in this case.
Member 15627495 5-Feb-23 9:42am    
the origin of the text involved matters too.
What is the origin of the new text ? a web page ?
Is the new content 'reliable' for the aim requested ?

depending the source text, holding the text format could be impossible
Dave Kreskowiak 5-Feb-23 12:56pm    
The "source" of the text does not matter.
Member 10631275 5-Feb-23 16:11pm    
it's only text with back color and some size.

Use the RichTextBox.AppendText method:
MyRichTextBox.AppendText("Hello world\n");
Share this answer
thanks but i need to add and change txt in the middle not the end...
Share this answer
Dave Kreskowiak 5-Feb-23 12:54pm    
First, you posted this as an answer to your own question. If you need to reply to a post, click the "Have a question or Comment" button under the answer you want to reply to. That way, the person gets an email that says you replied to them.

Next, which type of app are you writing? Windows Forms, WPF, ASP.NET WebForms, MVC?, ...
Member 10631275 5-Feb-23 16:08pm    
windows form
ok, What worked for me was is richTextBox1.SelectedText+="what you need to add"; it's tricky but it's work.
Share this answer

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