StreamReader question


May 12, 2005
Programming Experience
Good Evening,

I have just signed up in hopes of finding a good explanation to my question. I would like to thank everyone in advance for any help they could give.


I am trying to create a "Previous Record" button for a windows form.

Current Setup:

i have a File menu with 2 menu items ( OPEN , EXIT )
i have 2 labels for display purposes ( nameLabel , phoneLabel )
and i have 2 buttons ( NEXT , PREV )

i am using the StreamReader to open up and read from a txt file.

Here is the code that i have on the form.

Option Strict On

Imports System.IO

Public Class Form1
	Inherits System.Windows.Forms.Form

Private phoneStreamReader As StreamReader

	Private Sub fileOpenMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fileOpenMenuItem.Click
		'Open the file
		Dim response As DialogResult
		OpenFileDialog1.InitialDirectory = Application.StartupPath
		response = OpenFileDialog1.ShowDialog()
		If response <> DialogResult.Cancel Then
		    phoneStreamReader = New StreamReader(OpenFileDialog1.FileName)
			nextButton_Click(sender, e)
		End If

	End Sub

	Private Sub nextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nextButton.Click
		Dim test As Long
		If phoneStreamReader.Peek <> -1 Then
			nameLabel.Text = phoneStreamReader.ReadLine()
			phoneLabel.Text = phoneStreamReader.ReadLine()
		End If
	End Sub

	Private Sub prevButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles prevButton.Click
	End Sub
End Class

As the code shows, i am asking the user to specify the TXT file, then the file is open and read. The first 2 lines are then assigned to the Labels.Text

My problem is i would like to setup a previous button to go "backwards" in the txt file.

Is this possible?

Thank You



VB.NET Forum Moderator
Staff member
Jun 3, 2004
Lansing, MI; USA
Programming Experience
since the streamreader only reads in one direction (beggining to end)
you could have the streamreader read the entire txt file into a list collection that way to go "back" just get the info from the collection

collections have an index that you can use to move foreward (index + 1) or backwards (index - 1)