Reading form a text file

Mr Bean

Member
Joined
Feb 5, 2008
Messages
5
Programming Experience
Beginner
I am making a windows form in vb.net that needs to read form a text file and insert the text as objects when the form loads . The textboxes are as follow

TxtJobtitle.Text
TxtSalary.Text
TxtFullname.Text
TxtAdd1.Text
TxtAdd2.Text
TxtTown.Text
TxtCounty.Text
TxtPostcode.Text
TxtPhone.Text
TxtEmail.Text

i have this code to save it to the text file (not sure if its correct)
Private Sub CmdSaveall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSaveall.Click
Dim sw As New StreamWriter("Staff.txt")
counter = 0
While counter < collection.Length
sw.WriteLine(collection(counter).Jobtitle)
sw.WriteLine(collection(counter).Salary)
sw.WriteLine(collection(counter).Fullname)
sw.WriteLine(collection(counter).Add1)
sw.WriteLine(collection(counter).Add2)
sw.WriteLine(collection(counter).Town)
sw.WriteLine(collection(counter).County)
sw.WriteLine(collection(counter).Postcode)
sw.WriteLine(collection(counter).Phone)
sw.WriteLine(collection(counter).Email)

counter += 1
End While
sw.Close()

So what would be the best way to do this been trying to do it for 5 hours now
 
What I would do is make a routine (another button) that clears the collection (use Collection.Clear) then use code similar to this (you'll need to modify it a little):
VB.NET:
Dim sr As New StreamReader("Staff.txt")
While sr.Peek <> -1
  collection(counter).Jobtitle = sr.ReadLine()
  collection(counter).Salary = sr.ReadLine()
  collection(counter).Fullname = sr.ReadLine()
  collection(counter).Add1 = sr.ReadLine()
  collection(counter).Add2 = sr.ReadLine()
  collection(counter).Town = sr.ReadLine()
  collection(counter).County = sr.ReadLine()
  collection(counter).Postcode = sr.ReadLine()
  collection(counter).Phone = sr.ReadLine()
  collection(counter).Email = sr.ReadLine()
End While
sr.Close()
the 'collection(counter)' part is where you add a new object to the collection, I don't know what type it is, but you use the same concept as when you add something to it from the textboxes
 
Ok i so if i put something like
TxtJobtitle.Text = ""
then the code you recommmed with the the type which i am using which i think would be this
Private Sub CmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdAdd.Click
Dim client As New Staff()
client.Jobtitle = TxtJobtitle.Text

take the client part and add collection(counter).Jobtitle = sr.ReadLine()

client.Jobtitle.collection(counter).Jobtitle = sr.ReadLine()

is that what you meant
sorry about this quite new to vb.net
 
It would be a little more like:
VB.NET:
Dim client As New Staff
client.Jobtitle = sr.ReadLine()

Collection.Add(client)
 
so if used
Dim client As New Staff
client.Jobtitle = sr.ReadLine()

Collection.Add(client)

and added the others the same way would i need to add another Collection.Add(client)

For example

Dim client As New Staff
client.Jobtitle = sr.ReadLine()
client.Salary=sr.ReadLine()
Collection.Add(client)
Collection.Add(cleint) or would this line only needed to be added once

and thanks for helping i am very thankfull for your help
 
VB.NET:
While sr.Peek <> -1
  client.Jobtitle = sr.ReadLine()
  client.Salary = sr.ReadLine()
  client.Fullname = sr.ReadLine()
  client.Add1 = sr.ReadLine()
  client.Add2 = sr.ReadLine()
  client.Town = sr.ReadLine()
  client.County = sr.ReadLine()
  client.Postcode = sr.ReadLine()
  client.Phone = sr.ReadLine()
  client.Email = sr.ReadLine()
  Collection.Add(client)
End While
 
Back
Top