so my project is a text editor and i am trying to make an open button and save. my code only opens the last part of the file it read. i was wondering if someone good help me or give any tips. thanks

Private Sub OpenToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim fileString As String
        Dim openStreamReader As StreamReader = New StreamReader("file.txt")
        Dim reponseDialog As DialogResult

        If openStreamReader IsNot Nothing Then
            openStreamReader.Close()
        End If

        With OpenFileDialog1
            .InitialDirectory = Directory.GetCurrentDirectory
            .FileName = "file.txt"
            .Title = "Select File or Directory for File"
            reponseDialog = .ShowDialog()
            If reponseDialog <> DialogResult.Cancel Then
                openStreamReader = New StreamReader(OpenFileDialog1.FileName)
            End If
        End With

        Do Until openStreamReader.Peek = -1
            fileString = openStreamReader.ReadLine() & Environment.NewLine
            RichTextBox.Text = fileString
        Loop

    End Sub

my code only opens the last part of the file it read

Actually only the last line. RichTextBox.Text = fileString replaces the content of the richtextbox each time you read the next line. You should append lines so the previous content is not lost

RichTextBox.AppendText(fileString)

You could also check My.Computer.FileSystem.ReadAllText and My.Computer.FileSystem.WriteAllText methods for a simple text file loading and saving.

This article has been dead for over six months. Start a new discussion instead.