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
 
The problem with your code is that the MessageBox call is inside your loop instead of being after :

You can try this
VB.NET:
[SIZE=2]

[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button2_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button2.Click[/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sReader [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IO.StreamReader
sReader = IO.File.OpenText([/SIZE][SIZE=2][COLOR=#800000]"C:\Towns.txt"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Data [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Towns [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2]Data = sReader.ReadLine()

[/SIZE][SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE][INDENT][SIZE=2]Data = sReader.ReadLine()
Towns &= Data & vbCr
[/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]Loop[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Until[/COLOR][/SIZE][SIZE=2] Data [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2]sReader.Close()
MessageBox.Show(Towns)
[/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

If you want to display the result in a textbox, you can easily replace the MessageBox call by something like TextBox1.text=Towns.

Hope it helps
 
Thanks Bob, A great help. From what little I have seen of this subject I changed 'vbCr' to 'vbCrLf' - just as an experiment. Fine!!
The more I get into dotNET the more I am amazed at the different methods.
I had a quick peep at your profile: My birthday is close to yours. Me: 16th October.
Thanks, again.
 
IO.Streamer

Hi. I am trying to get a bit further but have got stuck. I believe the snag lies here:-
VB.NET:
Data = sReader.ReadLine()
Towns &= Data
This gives me the first line from the text file(Same one as before) and puts it into a listbox. Every time I click the button I get the same line from the file and puts it on the next line in the list box. (Which is fairly obvious really.) What I want to do is get the first line of the file, then the next, and so on with each button click. Am I right in thinking it is those lines I show that will, with some alteration, do it? I have been trying all sorts of things that I know of, but I do not know enough of what you can do in this sphere. I was thinking in the way of using a 'count' on successive button clicks, but I don't know how to use it with extracting info from a text file.
Can someone help me please.
Yours Truly.
 
Here is the online documentation for Streamreaders ReadLine method:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiostreamreaderclassreadlinetopic.asp
You have it locally too, "Help" on the top menu, or highlight the word "ReadLine" and press the "F1" function key on your keyboard.

Now, how about this code to add all text lines of that file to ListBox1 ?
VB.NET:
Private Sub Button2_Click(sender, e ) Handles Button2.Click
  ListBox1.Items.Clear()
  Dim sReader As New IO.StreamReader("C:\Towns.txt")
  Do While sReader.Peek <> -1
    ListBox1.Items.Add(sReader.ReadLine())
  Loop
  sReader.Close()
End Sub
 
IO.StreamReader

Hi. Thanks for your help. That code works well. I do still need some help, if that is alright to ask? I took the advice given and looked in MSDN. I copied code and 'played' with it - but I am still stuck!! I am sure the answer is something simple. But it is something beyond my grasp.
With this code that follows I can get the first line from the file into my listbox.
VB.NET:
Dim sReader As IO.StreamReader
sReader = IO.File.OpenText("C:\Towns.txt")
Dim Data As String
Dim Towns As String = ""
 
Data = sReader.ReadLine()   [COLOR=magenta]*****[/COLOR]
 
Towns &= Data
sReader.Close()
Debug.Write(Towns)
lstTowns.Items.Add(Towns)
If I click the button again, I get the same line in the listbox underneath the first one.
What I want is that when I click the button again, I get the next line from the file added to the one before. And a third click gets the third line from the file....and so on.
[A strange thing is that with that code, above, if that line marked with stars is put in twice I get the second line from the file!!] [Three lines of that I get the third from the file!]
You see! I think that it is the coding of that particular line? No? Yes?
Later on, I shall want to pick out any line from the file and display it. I think that may be connected in using same type of coding.
By the way! I put that line in:
VB.NET:
Private Sub Button2_Click(sender, e ) Handles Button2.Click
And got this:
Method 'Button2_Click' cannot handle Event 'Click' because they do not have the same signature.
If I am a bother, Gentlemen, please say so.
When I am a boffin I can help others. :D
Have a nice week.
Incidently, the file 'Towns' is a list of those in the UK.
Cheers and Thanks.
 
You have got it, each call to ReadLine will return the next line from file (if there is one).
As stated in documentation, it will always start with first line when StreamReader is initialized, so what shall you do if you want only one line each button click and always the next one? well you have initialize the StreamReader outside that button click event handler then, don't you think?

Second question, I obviously shortened the long and winding road to the standard "sender, e" parameters. Do you see the connection?
 
Hi JohnH. I apologise if I came across rude to you. 2nd first: I thought you shortened it, but being new to vb I am seeing code that I wouldn't have believed possible!
Being new, means also that it is the code to put for taking out from file one line at a time that I am lost.
Thanks.
 
You don't really mean to click a button for each town there is in the UK to add it to a listbox? What is the point of that?
 
It is for an exercise in the manipulation of files. It could have been animals.
By the way, thanks for your hint. I got the required one-by-one into the listbox. And do you know what? I've got stuck with working out what code to use for end of file. I've tried everything I know. Do While...Do Until...
EOF... If....<>.... If .... = ... ! Put in a count so I know when to stop!!
But that's no good!!
Got any ideas, please. I'm bewildered. I'm teaching myself and I only have chaps like you.
Thanks a great deal for your assistance... Have a good week.
 
That'd be the Peek method of StreamReader. See post 5. As the documentation says, Peek checks if there is a next character to be read in the file, if there is not this method will return -1. So have a go with sReader.Peek <> -1
 
Can either of you two explain to me how to use a Streamreader and a Streamwriter? I have figuered out alot from reading your posts, but my form has about 35 controls I must save the information for. As TwoWing said, I'm teaching my self, so all I have is guys like you!
 
I can only highly recommend the .Net Framework documentation locally/online by MSDN library. It is easy to browser/search the namespaces and all their classes. Read the accurate documentation about the classes/methods/properties. Read and try out the massive amount of simple examples accompanying almost every aspect of the docs. Did I say try it out? Also utilize your favorite search engine to lookup tutorials and articles about specific topics. And do ask questions at forums etc like this when you have specific question about stuff you can't make work, or if you can't find the development tool to do a task. I don't think there exist a forum where you can go to and ask anyone to give you a private lesson about a topic. For this people publish the mentioned numerous tutorials, articles and books.

Here is the best start you can get about the classes in question:
StreamReader http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemIOStreamReaderClassTopic.asp
StreamWriter http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemIOStreamWriterClassTopic.asp
 
IO.StreamReader

Hi. For the following I have declared 'data' and 'sReader' as global.
Button_Click
VB.NET:
data = sReader.ReadLine()
Debug.WriteLine(data)
If sReader.Peek <> -1 Then
lstTowns.Items.Add(data)
End If
sReader.Close()
End Sub
It brings out the first line from file and puts it in listbox. But on the next button click prog crashes with:- "cannot read from closed TextReader".
I 'took out' line 'sReader.Close()' and the prog worked putting every line, except the last, from the file into the listbox. What now? The file has to be closed somehow!
With regards to MSDN, I have had some good info from it, but like manuals on dotNET one needs a guide and it tends to stop short leaving one at a loose end. That is why this Forum is here. Yes?
Good work lads and lasses.
Rome wasn't burnt in a day!
Leo Rosten. :)
 
Back
Top