Read a txt file into a Richtextbox

snssewell

New member
Joined
Jan 15, 2008
Messages
1
Programming Experience
Beginner
I am trying to read a txt file into a richtextbox and been doing some research and from what I read this should work, but I get a error saying that the file format is not valid. I thought that the richtextbox control can read txt files as well as rtf files automatically. The file I am trying to read is saved in Notepad as a txt file. Any Ideas why it wouldn't work? Here is what I got so far.

VB.NET:
Option Strict On
Option Explicit On

Public Class frmMain

    Private Sub OpenFile()
        Dim Result As DialogResult
        Result = ofdMain.ShowDialog
        If Result = Windows.Forms.DialogResult.OK Then
            rtbNotes.LoadFile(ofdMain.FileName, RichTextBoxStreamType.RichText)
        End If
        
        tsslFileName.Text = ofdMain.FileName
    End Sub

Private Sub msOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles msOpen.Click
        Call OpenFile()
    End Sub
Any help will be appreciated.
 
Last edited by a moderator:
Try changing the rtbNotes.loadFile line from this:

VB.NET:
rtbNotes.LoadFile(ofdMain.FileName, RichTextBoxStreamType.RichText)

To this:

VB.NET:
rtbNotes.LoadFile(ofdMain.FileName, RichTextBoxStreamType.PlainText)

That should allow you to open a plain text file. That is really the extent of my work with rich textboxes, but when I tried your code with that change it worked for me.
 
Last edited:
Back
Top