-1
Private Sub TextFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextFileToolStripMenuItem.Click

        fd.Filter = "Text Files (*.txt)|*.txt"
        If fd.ShowDialog() = DialogResult.OK Then
            FileName = fd.FileName
            Ext = IO.Path.GetExtension(FileName)
            'read file
            If System.IO.File.Exists(FileName) = True Then
                Dim objReader As New System.IO.StreamReader(FileName)
                Do While objReader.Peek() <> -1
                    TextLine = TextLine & objReader.ReadLine & vbNewLine
                Loop
                TextFile.Text = TextLine
            Else
                MsgBox("File Does Not Exist")
            End If
            Me.BtnSort.Enabled = True
            Me.BtnDestroy.Enabled = True
            'counter = counter + 1
        End If
End Sub
Florian Schmidinger
  • 4,377
  • 2
  • 13
  • 25

2 Answers2

0

My guess is that the reference named fd is set to null (nothing) so you need to instantiate a new OpenFileDialog and assign it to the reference:

fd = New OpenFileDialog()

Since you seem to want to read the whole text of the file anyway you can shorten this code (File.Exists allready returns a boolean value so you dont need to compare with true):

If System.IO.File.Exists(FileName) Then
    TextLine = TextLine & System.IO.File.ReadAllText(FileName)
    TextFile.Text = TextLine
Else
Florian Schmidinger
  • 4,377
  • 2
  • 13
  • 25
0

you need to create an instance for your fd before to use it. Maybe in te constructor or in it's declaration itself:

Private fd As New Form1
alex
  • 313
  • 1
  • 2
  • 16