I have recently began learning VB.NET, I have however ran into a problem. I have been trying to create a lottery generating program. My numbers that are generated are stored into a text file, I want to however display the last set of numbers generated by reading from this text file and input them into a text box on my form. I have came up with the following code, there are no errors, but no numbers are read from the file either:
[code]Dim lines() As String = System.IO.File.ReadAllLines("lottery_draws.txt")
Dim objReader As New System.IO.StreamReader("lottery_draws.txt")
If System.IO.File.Exists("lottery_draws.txt") = True Then
Do While objReader.Peek() <> -1
lines(1) = lines(1) & objReader.ReadLine() & vbNewLine
lines(2) = lines(2) & objReader.ReadLine() & vbNewLine
lines(3) = lines(3) & objReader.ReadLine() & vbNewLine
lines(4) = lines(4) & objReader.ReadLine() & vbNewLine
lines(5) = lines(5) & objReader.ReadLine() & vbNewLine
lines(6) = lines(6) & objReader.ReadLine() & vbNewLine
lines(7) = lines(7) & objReader.ReadLine() & vbNewLine
objReader.Close()
Loop
End If
lblDate1.Text = lines(1) & objReader.ReadLine() & vbNewLine
tbNumber1_1.Text = lines(2) & objReader.ReadLine() & vbNewLine
tbNumber1_2.Text = lines(3) & objReader.ReadLine() & vbNewLine
tbNumber1_3.Text = lines(4) & objReader.ReadLine() & vbNewLine
tbNumber1_4.Text = lines(5) & objReader.ReadLine() & vbNewLine
tbNumber1_5.Text = lines(6) & objReader.ReadLine() & vbNewLine
tbBonus1.Text = lines(7) & objReader.ReadLine() & vbNewLine[/code]
The text file that it is trying to read from is formatted like so:
[code]09/12/2010
1
15
16
19
36
44[/code]
Could anyone give me any pointers or help on where I am going wrong?
You're checking that the file exists, after you've already attempted to open the file twice, not for any real good reason I can assume.
You're reading the lines in the file into an array, then re-reading the file and appending the same data to every var so it says the same thing twice for every line.
You close your stream reader, then try to read from the same already closed reader 7 times, attempting to append the same data a third time to every var in your array as you set your textbox values.
You're writing new lines into single-line textboxes from the look of it too.
Sorry, you need to Log In to post a reply to this thread.