Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This is a solution, not a question.

I've found no mention of this anywhere, so eventually came up with my own solution. Annoyingly enough, the richtextbox does not allow plain text in its richtextbox.RTF property. So even placing the value from richtextbox.RTF into another box, then placing those contents back into the richtextbox.RTF does not work.

Here is the conversion code (somewhat of a workaround) that converts plain text into a richtext format:

VB
Private Function ConvertTextToRTF(ByVal RTFtext As String) As String
      Dim data_object As New DataObject
      data_object.SetData(DataFormats.Rtf, RTFtext)
      Return data_object.GetData(DataFormats.Rtf)
End Function


Use as follows:
VB
Dim text as string = "Hello World"
richtextbox.RTF = ConvertTextToRTF(text)
Posted
Updated 5-Jan-17 11:36am
Comments
Henry Minute 19-Feb-11 20:32pm    
If you are not asking a question, then please do not post in this forum.

There is a Tips/Tricks forum here on The Code Project, which is the correct place to post things like this.

You will find it here: http://www.codeproject.com/Tips/post.aspx
shaneapp 20-Feb-11 18:14pm    
Sorry sorry...first time I've posted and just wanted to get this solution out quick (busy at the moment)

Okay, I don't get it. Why use the RTF property for plain text? Why not use the Text property?

C#
richTextBox1.Text = "Plain text";
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Feb-11 19:32pm    
My 5, I think this is all OP needs.
--SA
Nish Nishant 19-Feb-11 19:33pm    
Yes, I think he's over-thinking this and going for a complex solution when a simple one exists.
Nish Nishant 19-Feb-11 19:33pm    
Thanks SA.
Abhinav S 19-Feb-11 22:58pm    
See my comments below. :)
Espen Harlinn 20-Feb-11 10:34am    
Nice and simple :)
You might need to understand some of the differences between richtext and plain text.
Rich text.

Rich text uses only an 8 bit format which allows it to use only unicode characters. However escape sequences for e.g. \u will tell the converter what to do.

To convert plain text to RTF, you will need to include the approrpriate rtf tags in plain text to make them work in rtf.

See here[^] for a more detailed analysis.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Feb-11 23:23pm    
Yes, very important! My 5.
(It's very good you reply my comments like that, otherwise I would not notice such Answers as I wouldn't get e-mail notification, hope others understand that, too. Thank you.)
--SA
Sergey Alexandrovich Kryukov 19-Feb-11 23:26pm    
What you mention is actually an essential problem of hard pre-Unicode legacy. I remember I had hard time solving very tricky problem of making ANSI-oriented Borland VCL with Unicode; big part of it was RichText-specific: how to get Unicode text.
--SA
Abhinav S 19-Feb-11 23:53pm    
Yeah. I remember I had a tough time getting the original text (out of rtf) when I wanted to do it without using a work around similar to the one the OP has used.

I finally gave up and used the workaround :).
Sergey Alexandrovich Kryukov 20-Feb-11 0:08am    
But I resolved it (will have trouble to remember the details). The article was printed in "The Delphi Magazine" (Web and on paper magazine) and was pretty popular, and I enjoyed good author's payment :-)
--SA
Abhinav S 19-Feb-11 23:54pm    
Yes. I infact, got this idea from you. :)
I tried the solution posted by 'shaneapp', but I never got it to work, I even Copied the Code directly, but when I tried to Save the File, it would give me an "Invalid file format" error.
After experimenting, I stumbled across this very easy solution that works:

<pre>   Dim text As String = "Scott"
        ' By doing SelectedText - it converts it to RichTextFormat ...
        '' (As you will be able to see if you Open the file ...
        '' generated below - it contains the rtf format codes.
        RichTextBox1.SelectedText = text
        RichTextBox1.SaveFile("C:\Test\test1.rtf")
 
Share this answer
 
Comments
Dave Kreskowiak 5-Jan-17 20:45pm    
First, you posted this as a solution to a FIVE YEAR OLD non-question. DO NOT DO THIS! Post it as a tip/trick instead, just like was described five years ago to 'shaneapp'.

Second, his "solution" is not a solution to any problem at all.

Also, your "solution" is really not a solution either as it's pretty much common sense. Just reading the documentation on the RichTextBox control would have told you this.

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