Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB.net RichTextBox File format is not valid

Hi. I am trying to save the contents of a RichTextBox to a .rtf file and reload it, but am getting the error
"VB.net 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
        SaveMyFile()
    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, _
                RichTextBoxStreamType.PlainText)
        End If
    End Sub
    Private Sub btnLoad_Click(sender As System.Object, e As System.EventArgs) Handles btnLoad.Click
        CreateRichTextBox()
    End Sub

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

        'richTextBox1.lo()
        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)

        Me.Controls.Add(richTextBox1)
    End Sub
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Specify PlainText, just like when you saved the file:

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

ASKER

Hi. Thanks. If I specify plain text then will I be able to reload the text as formatted?
How would I change the code?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks very much