I need to programmatically add a new paragraph to a RichTextBox control (just like pressing Enter). Using the code below, it does add a new paragraph but:
- It deletes all the existing text in the control
- The insertion point remains in the first line and doesn't move to the second newly created line
It only seems to add a new paragraph once, i.e. if I run the code a second time, a third paragraph isn't created
FlowDocument flowDoc= rtbTextContainer.Document; Paragraph pr = new Paragraph(); flowDoc.Blocks.Add(pr); rtbTextContainer.Document = flowDoc;
I was testing a few things - I commented out the second and third lines of code, so I was only reading the Document and immediately set it back to the RichTextBox again, but that also deleted all existing text, so the issue might have something to do with that, but I can't figure it out.
How can I overcome these issues and programmatically append a new paragraph, and then set its focus.
Thanks
FlowDocument
-- and the text in the control was not deleted. Is there something else going on here not shown in the question? Maybe a data binding? – dbc