Hello, I have problem in one of my program,it's almost a copycat of MS-World, that means it's using RichTextBox, I have Bold,Italic,Underline etc. buttons but when I save the file and open it, it's a basic text that you can get from Notepad.

File Type: DTX
Save Method: Writer (User-made save prompt)

how about this

string GetContentAsRTF(RichTextBox rtb)
{
    var range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);

    using (var stream = new MemoryStream())
    using (var reader = new StreamReader(stream))
    {
        range.Save(stream, DataFormats.Rtf);
        stream.Position = 0;
        return reader.ReadToEnd();
    }
}

though the file type is RTF

Edited 3 Years Ago by azareth: wrong post

Comments
Not a VB code.
That is not VB code.

Please post the code you are using to load and save the text. The usual way to load and save rich text is as follows:

Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
    RichTextBox1.SaveFile("d:\temp\myfile.rtf")
End Sub

Private Sub btnLoad_Click(sender As System.Object, e As System.EventArgs) Handles btnLoad.Click
    RichTextBox1.LoadFile("d:\temp\myfile.rtf")
End Sub

Edited 3 Years Ago by Reverend Jim

Comments
Use this if you want to save/load from/on RichTextBox

Thank you so much, Reverend Jim, I didn't know that RichTextBox has .Save and .Load features :-)
If you still need the code I was using...

               Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
                objWriter.Write(editor.RichTextBox1.Text)
                objWriter.Close()

(I was using objWriter to write the text from the richtextbox)

I didn't know that RichTextBox has .Save and .Load features

Neither did I when I first started using it. That's why I like to browse the intellisense methods from time to time. If this gives you what you need then please mark this thread solved.

Just a bit of info to add to the discussion.

Your original code will give you your expected result if you change
objWriter.Write(editor.RichTextBox1.Text)
to
objWriter.Write(editor.RichTextBox1.Rtf)

Those dang computers always try to do what you tell them to do, not what you want them to do. :)

That said, its easier to use the Save and Load methods as Jim has shown you.

Comments
Thanks.
This question has already been answered. Start a new discussion instead.