Click here to Skip to main content
14,667,947 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello


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).

Thanks,
Joseph.
Posted
Updated 11-Nov-10 3:11am
v3
Comments
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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


  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.
   
v2
Comments
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100