简体   繁体   中英

VB.Net Issue reading text to a textbox

I have a simple sticky notes application that I've been working on for a while now, and I've ran into a problem importing/loading in text to a textbox. Here is my current code:

Private Sub newNote_Click(sender As Object, e As EventArgs) Handles newNote.Click
    Dim newNoteFrm As New Notes
    newNoteFrm.Show()
End Sub

Private Sub exportNote_Click(sender As Object, e As EventArgs) Handles exportNote.Click
    Dim saveFile As New SaveFileDialog
    Dim myStream As Stream
    saveFile.Title = "Export/Save Note"
    saveFile.Filter = ".txt|*.txt|All Files|*.*"
    saveFile.FilterIndex = 0

    If saveFile.ShowDialog() = DialogResult.OK Then
        RichTextBox1.SaveFile(saveFile.FileName, RichTextBoxStreamType.RichText)
        Me.Text = saveFile.FileName.ToString
    End If

End Sub

Private Sub importNote_Click(sender As Object, e As EventArgs) Handles importNote.Click
    Dim openFIle As New OpenFileDialog
    openFIle.Filter = ".txt|*.txt|All Files|*.*"
    openFIle.FilterIndex = 0
    If openFIle.ShowDialog() = DialogResult.OK Then
        RichTextBox1.Text = IO.File.ReadAllText(openFIle.FileName)
    End If
End Sub

The issue comes at this part:

    Private Sub importNote_Click(sender As Object, e As EventArgs) Handles importNote.Click
    Dim openFIle As New OpenFileDialog
    openFIle.Filter = ".txt|*.txt|All Files|*.*"
    openFIle.FilterIndex = 0
    If openFIle.ShowDialog() = DialogResult.OK Then
        RichTextBox1.Text = IO.File.ReadAllText(openFIle.FileName)
    End If
End Sub

For some reason, when I export any file from this application as a .txt, I can't re-open it in the application without issue. This is what is happening when I re-open a file that I saved from this program specifically. (Note: If I save a .txt file from notepad, or wordpad, I can open it with no issue. It only occurs when I save it from my application and open it in my application.): This is what happens when I re-open a file:

{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Kristen ITC;}}
\viewkind4\uc1\pard\lang1033\f0\fs18 (my text that I saved shows here)\par
}

^ This is placed into the rich text box, and I'm unsure why. I'm assuming I'm saving files incorrectly? Thanks for any help

Notice how do you save your file?

 RichTextBox1.SaveFile(saveFile.FileName, RichTextBoxStreamType.RichText)

This save the file in RTF format, the name, (and the extension choosen), of the file has no effect to the format because you have used the RichTextboxStreamType.RichText option.

You should use the RichTextBox method that is able to understand the RTF text saved by the SaveFile method.

If openFIle.ShowDialog() = DialogResult.OK Then
    RichTextBox1.LoadFile(openFIle.FileName)
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM