Solved RichTextBox File format is not valid

Posted on 2012-09-22
Last Modified: 2012-09-28
Hi. I am trying to save the contents of a RichTextBox to a .rtf file and reload it, but am getting the error
" RichTextBox File format is not valid" at the line marked XXXX

Imports System.Windows.Forms

Public Class Form3

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
    End Sub

    Public Sub SaveMyFile()

        Dim saveFile1 As New SaveFileDialog()

        saveFile1.DefaultExt = "*.rtf"
        saveFile1.Filter = "RTF Files|*.rtf"

        If (saveFile1.ShowDialog() = System.Windows.Forms.DialogResult.OK) _
            And (saveFile1.FileName.Length) > 0 Then

            Me.Label1.Text = saveFile1.FileName

            RichTextBox1.SaveFile(saveFile1.FileName, _
        End If
    End Sub
    Private Sub btnLoad_Click(sender As System.Object, e As System.EventArgs) Handles btnLoad.Click
    End Sub

    Public Sub CreateRichTextBox()
        Dim richTextBox1 As New RichTextBox()
        richTextBox1.Dock = DockStyle.Fill

        richTextBox1.LoadFile("C:\Users\user\Desktop\v.rtf") 'XXXX
        richTextBox1.Find("Text", RichTextBoxFinds.MatchCase)

        'richTextBox1.SelectionFont = New Font("Verdana", 12, FontStyle.Bold)
        'richTextBox1.SelectionColor = Color.Red

        richTextBox1.SaveFile("C:\Users\user\Desktop\v.rtf", RichTextBoxStreamType.RichText)

    End Sub
Question by:murbro
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Specify PlainText, just like when you saved the file:

        richTextBox1.LoadFile("C:\Users\user\Desktop\v.rtf", RichTextBoxStreamType.PlainText)

    Author Comment

    Hi. Thanks. If I specify plain text then will I be able to reload the text as formatted?
    How would I change the code?
    LVL 85

    Accepted Solution

    ...but it is plain text, since you saved it with:

        RichTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText)

    If you want it to save/load formatted, then don't specify plain text when you save it!  =)

    Author Closing Comment

    thanks very much

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now