Question Working with text files

dominion_vortar

New member
Joined
Jan 2, 2010
Messages
4
Programming Experience
Beginner
Hi,

I have a text file containing 10 pieces of information.

I want to read the information from the text file and put it in text boxes on a form, so that the user can view the information and edit if required.

What is the best way of approaching this?

Thanks for your time :)
 
You can use the OpenTextFileReader

Assuming each of the ten pieces of information is on a different line of your text file then the following works for me:

Sub ReadFile()
Dim MyFile As Object​

'FIRST OPEN THE FILE FOR READING
MyFile = My.Computer.FileSystem.OpenTextFileReader("C:\TestFile.txt")​

'THEN, LINE BY LINE READ THE FILE INTO EACH TEXT BOX
me.TextBox1.Text=MyFile.readline()
me.TextBox2.Text=MyFile.readline()
me.TextBox3.Text=MyFile.readline()
me.TextBox4.Text=MyFile.readline()
me.TextBox5.Text=MyFile.readline()
me.TextBox6.Text=MyFile.readline()
me.TextBox7.Text=MyFile.readline()
me.TextBox8.Text=MyFile.readline()
me.TextBox9.Text=MyFile.readline()
me.TextBox10.Text=MyFile.readline()​
End sub

Of course this just reads a specific file. You need to decide whether this is hard coded as in the above (ie C:\TestFile.txt) or if the user should select it using an open file dialog. It also does no checking to see if the file is valid for the purpose, that would depend on what type of information is expected by your program.
 
Thanks very much for that, I've managed to load information into text boxes with their corresponding prices.

Is there a way to amend the code so that when the user, for example, pressed "Save" after altering the values in the text boxes, it writes over that information in the file?
 
Use OpenTextFileWriter

Firstly note that after reading in the data from the file we should close it using:

VB.NET:
MyFile.close()

We used the OpenFileTextReader to Read the file. You can similarly use the OpenTextFileWriter to write to it as follows (this assumes you use a button named SaveButton and then handle its click event):

VB.NET:
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
Dim MyFile As Object
[INDENT]'OPEN THE FILE FOR WRITING
MyFile = My.Computer.FileSystem.OpenTextFileWriter("C:\TestFile.txt", False)
'LINE BY LINE WRITE THE NEW DATA
MyFile.Writeline(Me.TextBox1.Text)
MyFile.Writeline(Me.TextBox2.Text)
MyFile.Writeline(Me.TextBox3.Text)
MyFile.Writeline(Me.TextBox4.Text)
MyFile.Writeline(Me.TextBox5.Text)
MyFile.Writeline(Me.TextBox6.Text)
MyFile.Writeline(Me.TextBox7.Text)
MyFile.Writeline(Me.TextBox8.Text)
MyFile.Writeline(Me.TextBox9.Text)
MyFile.Writeline(Me.TextBox10.Text)
MyFile.close()[/INDENT]
End Sub

Note the 'False' at the end of the line for opening the file for writing. This is used to tell the writer to overwrite the file. 'True' is used to append data to a file.

Again this is code that simply writes to the file. It actually destroys the file and recreates it. So, all data needs to be written not just the changed data. Also it does no checking or validating of the data before it writes it of the file nor does it check if the user really wants to overwrite the existing file.

Hope this helps.
 
Back
Top