Runtime error, File Format is not valid in RichTextBox.LoadFile()

Hey,
I have a starnge runtime error, when I run my program and click ont he load option it opens up the load dialog, so then I click on Text FIle(*.txt), and then click on a text file to open and then when it is opening it, it gets a runtime error, it highlights this line of code:
RichTextBoxPrintCtrl1.LoadFile(openDialog.FileName)

This is the load code:
Private Sub openMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openMenuItem.Click

openDialog.Title = "Open File"
openDialog.Filter = "Rich Text Files (*.rtf)|*.rtf|Word Files (*.doc)|*.doc|Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
openDialog.FileName = ""
openDialog.FilterIndex = 0

openDialog.InitialDirectory = "MyDocuments"

openDialog.CheckFileExists = True
openDialog.CheckPathExists = True

If openDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

RichTextBoxPrintCtrl1.LoadFile(openDialog.FileName)

documentTitle = openDialog.FileName

End If

End Sub


and this is the runtime error:
File Format is not valid


I am using Visual Basic 2005 express

Thanks :)



Answer this question

Runtime error, File Format is not valid in RichTextBox.LoadFile()

  • TRS-Bo

    You will need to import System.IO namespace

    Your richtextbox might be named different.

    If f.Extension = ".txt" Then
    RichTextBox1.LoadFile(f.FullName, RichTextBoxStreamType.PlainText)
    Else If f.Extension = ".rtf" Then
    RichTextBox1.LoadFile(f.FullName, RichTextBoxStreamType.RichText)
    End If



  • mdfq10

    If you want you can send me your project and I will have a look.

    afjohansson(at)hotmail.com
    replace (at) with @



  • Lbrsjhold

    So it should look like this:

    Dim f As FileInfo = New FileInfo("c:\test.txt")
    If f.Extension = ".txt" Then
    RichTextBoxPrintCtrl1.LoadFile(f.FullName, RichTextBoxStreamType.PlainText)
    ElseIf f.Extension = ".rtf" Then
    RichTextBoxPrintCtrl1.LoadFile(f.FullName, RichTextBoxStreamType.RichText)
    End If



    Before form1 class is started I put Imports System.IO.FileInfo
    so that is done, but I have 1 error now...and it is in:
    Dim f As FileInfo = New FileInfo("c:\test.txt")
    The first FileInfo has an error, Type "FileInfo" is not defined...


  • Pete Wojtkowiak

    This is what I did, I took out the .doc part, so now you can't open .doc files...So now you can only open rtf or txt...
    So now .txt works, but not rtf, so then I looked at the last link you gave me but that was for saving...so now what



    Thanks :)


  • tommaso_

    Ok, thanks it works now :)


  • Matt David

    http://msdn2.microsoft.com/en-us/3f99sst7(VS.80).aspx

    With this version of the LoadFile method, if the file being loaded is not an RTF document, an exception will occur. To load a different type of file such as an ASCII text file, use the other versions of this method that accept a value from the RichTextBoxStreamType enumeration as a parameter.

    You will need to use this method instead
    http://msdn2.microsoft.com/en-us/d76176b1(VS.80).aspx

    Depending on the file extension you will need to set the RichTextBoxStreamType to plain text or RTF
    http://msdn2.microsoft.com/en-us/system.windows.forms.richtextboxstreamtype.aspx



  • cmtytest2

    Ok,
    but when I open a rtf that has the words testing 123 123 in it this is what comes out:
    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
    \viewkind4\uc1\pard\f0\fs20 testing 123 123\par
    }
    but when I open a .txt it comes out fine...

    This is the code I have:

        Private Sub openMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openMenuItem.Click

            openDialog.Title = "Open File"
            openDialog.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            openDialog.FileName = ""
            openDialog.FilterIndex = 0

            openDialog.InitialDirectory = "MyDocuments"

            openDialog.CheckFileExists = True
            openDialog.CheckPathExists = True

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

                RichTextBoxPrintCtrl1.LoadFile(openDialog.FileName, RichTextBoxStreamType.PlainText)

            End If

        End Sub




  • FlyingHorse

    Here is some code that works well for me in VBEE

    Dim f As FileInfo = New FileInfo("c:\test.txt")
    If f.Extension = ".txt" Then
    RichTextBox1.LoadFile(f.FullName, RichTextBoxStreamType.PlainText)
    End If

    You will need to extend it to recognize other valid formats and what RichTextBoxStreamType to use.



  • PhilipRieck

    Dim f As FileInfo = New FileInfo("c:\test.txt")
    If f.Extension = ".txt" Then
    RichTextBox1.LoadFile(f.FullName, RichTextBoxStreamType.PlainText)
    End If

    That has errors in it...


  • Feret

    i tried the same thing at my end, it is working absolutely fine at my end. there is no problem in your code.



  • Kelly S

    Hi,

    Please mark reply as answer, which can help other member's if they have similar problem.

    Thank you,
    Bhanu.



  • Runtime error, File Format is not valid in RichTextBox.LoadFile()