Member Avatar for DyO152

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)

Recommended Answers

All 5 Replies

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

commented: That is not VB code. -3
commented: Not a VB code. +0

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
commented: Use this if you want to save/load from/on RichTextBox +0
Member Avatar for DyO152

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.

commented: Thanks. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.