IO.Streamreader

TwoWing

Well-known member
Joined
Jan 6, 2006
Messages
71
Location
Near Portsmouth, England
Programming Experience
Beginner
Hello Friends,
I bet the answer to this is easy - but I don't know it!
Can someone please help with this? With the following Code I can get all the towns listed in the file with the MessageBox. But only one at a time. Can someone please give me the Code to get the whole lot in a TextBox (Multiline set to True), line after line.
VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sReader As IO.StreamReader
sReader = IO.File.OpenText("C:\Towns.txt")
Dim data As String
data = sReader.ReadLine()
Do While data <> ""
MessageBox.Show(data)
data = sReader.ReadLine()
Loop
sReader.Close()
End Sub
I have earnestly tried different ways.
Also, I want to go a lot deeper into this topic, so if there are any pointers please can you indicate me in the right direction.
Sincerely, TwoWing
 
TwoWing, you must not try to ReadLine unless you first have checked with Peek that it's Ok.

About Close, yes, that would be appropriate at some point, either when you have read the last line (oh Peek :)) if you ever get there, or before you exit the application if the file is still open.
 
IO.Streamreader +

JohnH.
If you know the answer to my query could you please let me know without playing about. I qualified as a electro/mech engineer many years ago and am versed into seeking solutions to problems. If I am unable to find what I need in what I have to hand and libraries and net(even in MSDN), I therefore put the question to this forum as a last resort.(I have had some great help from here.)
I lost 15 years due to illness and am trying to catch up. I was in the designing of (now, primitive) digital systems before the PC came out, and just before my illness was designing keyboards to each manufacture's specifications.
I have recently built an accounting program[for family] of some 22MB in size and it is going through its 9th week of trial to iron out snags.[So far it has only been content of message boxes.]
Because of the lack of 'simple-to-understand' gen I am trying to build up a 'reference-room' of dotNET material so I can help others in places like this. Not for personal gain, but because I like to impart knowledge. But it can only be in a few spheres.
I would be most appreciative of all genuine assistance.
Truly Yours, TwoWing. :cool:
scottbogart: It is expensive (I had it on load from Public Library) but a book to have a look at, is :"Programming in MS VB.NET" by Francesco Balena. The section on Graphics is terrific.
 
I am not playing about, but I do seem to have wasted my time with you. See post 5, there is your code example. Use it, this is the usual use of the StreamReader. Your exercising does seem to file in the "special needs" section, regarding clicking a button through a file line by line to add it to a listbox just doesn't make sense. Now go load all those towns to the listbox with the code I gave you in post 5, and stop playing around.

Consider this thread abandoned by me.
 
perhaps this will help, i simply modified the code from this post made by TwoWing: http://www.vbdotnetforums.com/showpost.php?p=25251&postcount=15

VB.NET:
  If sReader.Peek <> -1 Then
    data = sReader.ReadLine()
    Debug.WriteLine(data)
    lstTowns.Items.Add(data)
  End If
sReader.Close()
End Sub

what this does is first check to see if there is another line in the text file, if there is ( hence the peek <> -1) then it reads the next line and adds it to the listbox
you can also put this into a do while loop too:
VB.NET:
lstTowns.Items.Clear()

Do While sReader.Peek <> -1
  lstTowns.Items.Add(sReader.ReadLine())
Loop
 
Back
Top