Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1503
  • Last Modified:

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
0
Murray Brown
Asked:
Murray Brown
  • 2
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Specify PlainText, just like when you saved the file:

    richTextBox1.LoadFile("C:\Users\user\Desktop\v.rtf", RichTextBoxStreamType.PlainText)
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi. Thanks. If I specify plain text then will I be able to reload the text as formatted?
How would I change the code?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
...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!  =)
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
thanks very much
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now