Click here to Skip to main content
14,732,778 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:

In short:
I have the need to extract the text from an RTF string, add some more text to it, and then convert it back into RTF.

In Detail:
I have one form with rich text box (say mainRichTextBox). In this main rich text box i have some text entered via another form (Say "InsertTextRtf" form).

I am just able to insert a text through "InsertTextRtf" form which has a one rich text box.

Now the case is :

mainRichTextBox have a following lines already entered:

Line 1: Hi hello all.

Line 2: Can you give me the solution.

Line 3: Thanks.

Now for mainRichTextBox there will be some RTF code.

Now I want to insert some text between Line 1 & Line 2. But this line will be inserted from "InsertTextRtf" with formatting.

I want to insert this text into "mainRichTextBox" with persisting formatting of both InsertTextRtf & mainRichTextBox.

If we take RTF of both using RTF property we can not simply insert into one another RTF's.

How can i insert a RTF text (RichTextBox) into another RTF text (RichTextBox).

Updated 11-Nov-10 4:11am
Kschuler 11-Nov-10 9:41am
Maybe you should post the code you have so far, and tell us what's not working. If you haven't started and are really lost, the best place to start is google.

1 solution

Set this as your homepage for your webbrowser, please note that it's URL is similar to your full classname;[^]

  1. You can use the Text[^] property to retrieve the text without any markup.
  2. You can use SelectionStart[^] and SelectionLength[^] to select a piece of text, say the one without markup that you inserted.
  3. You can then use SelectionColor[^] and SelectionFont[^] to apply a specific formatting to that selected piece of text. There's more ways of manipulating the RichTextBox, as there are also properties for alignment and indentation. Browse a bit on your new homepage, you'll find some very helpfull things.

My personal favorite would be SelectionProtected[^]
You can use this property to prevent the user from modifying sections of text within the control

Aw, as an additional suggestion, it would be handier to keep a lightweight version of the data in Memory. How a about a System.Generic.List<mymessage>, with MyMessage looking somewhat similar to like below?
public class MyMessage
    Public String EmailSender { get; set; }
    Public String MessageContent { get; set; }
    Public DateTime FellIntoOurInboxOn { get; set; }

Now, you would have the data somewhere in memory, and you have two views that you can manipulate at will, not only by content, but also in formatting.

A last quick tip; DetectURLS[^] is what you'd need if you want to have urls' in your RichTextBox - adding additional clickable functionality, so the user can invoke an action inlined with what he reads. The hyperlink click could be handled inside your application, if I recall correctly, could launch another application, or fetch a website.
Sandeep Mewara 1-Feb-11 10:31am
Great answer ans explanation. 5++

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