Search code examples
.netvb.netfile-iorichtextboxrtf

Saving a text as rtf using a RichtTextBox


I have a vb2010 application and want to save the text as a rtf document.

I do as follow:

Dim rtb As New RichTextBox
rtb.Text = "something"
rtb.SaveFile(myfile)

but the file is saved as an empty rtf document.

If I repeat the savefile line (line 3) again, it works and I get my rtf file document exactly as it should be.

Here the "working" code:

Dim rtb As New RichTextBox
rtb.Text = "something"
rtb.SaveFile(myfile)
rtb.SaveFile(myfile)

in other words it is like that the first SaveFile (line 3) instruction initialize the RichtTextBox and the next line (line 4) save the file.

Something is wrong, but I cannot find anything in those only 3 lines of code! Any suggestion? Thank you.


Solution

  • I advice you to get RTF from the property 'Rtf' and save it to file manually, for example in C#

    (sorry I'm C# programmer, not VB):

            RichTextBox rtb = new RichTextBox();
            rtb.Text = "Something";
            System.IO.File.WriteAllText(@"d:\Something.rtf", rtb.Rtf);