|
-
Sep 19th, 2010, 01:26 PM
#1
Thread Starter
Addicted Member
How to read text file line by line?
Ok, I saw this code on the net
Dim sFileName As String
Dim srFileReader As System.IO.StreamReader
Dim sInputLine As String
sFileName = "D:\Users\Arben\Desktop\SerieA.txt"
srFileReader = System.IO.File.OpenText(sFileName)
sInputLine = srFileReader.ReadLine()
Do Until sInputLine Is Nothing
System.Console.WriteLine(sInputLine)
sInputLine = srFileReader.ReadLine()
Loop
Now, I have some labels that I want the text on each of them to be the text on each line of the .txt file
now I write this
Label21.Text = sInputLine
Label22.Text = sInputLine
Label23.Text = sInputLine
under the code I pasted at the top, but I get the text on line1 in all labels, like all labels become 1, while the text should be 1, 2, 3 and so on. So anyone can help me do that, each label gets the value of the next line in the txt file
-
Sep 19th, 2010, 02:17 PM
#2
Lively Member
Re: How to read text file line by line?
all 3 labels are using the same variable, and it never changes which line to read
-
Sep 19th, 2010, 06:31 PM
#3
Re: How to read text file line by line?
try this:
vb Code:
dim lblCounter as integer = 1
Do while srFileReader.peek <> -1
'this will ouput text to label1, label2...etc
me.controls("Label" & lblCounter.tostring).text = sInputLine
lblCounter += 1
System.Console.WriteLine(sInputLine)
sInputLine = srFileReader.ReadLine()
Loop
Last edited by .paul.; Sep 19th, 2010 at 06:35 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 20th, 2010, 03:51 AM
#4
Fanatic Member
Re: How to read text file line by line?
@ paul you might want to check if the form actually contains a label of that name just in case. Also, using/endusing for streamreaders are a lot easier
vb Code:
Using sRead As New IO.StreamReader("D:\Users\Arben\Desktop\SerieA.txt")
Dim thisLine as string = ""
Dim nLabel as Integer = 0
Do Until sRead.EndOfStream
thisLine = sRead.ReadLine
if Me.Controls.ContainsKey("Label" & nLabel) then
Me.Controls("Label" & nLabel).Text = thisLine
End if
nLabel += 1
Loop
End using
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|