This code should get one random line from sample.txt and put it in Label1 - problem is that it's only getting line 2. Every single time. What's wrong with it? Please help. I bet it's something rediculously obvious but i can't find it :'(
Thank you so much in advance.
Note: This program will help me revise while playing WoW
Dim ioLine As String ' Going to hold one line at a time
Dim ioLines As String ' Going to hold whole file
Dim ioFile As New StreamReader("sample.txt")
ioLine = ioFile.ReadLine
ioLines = ioLine
'Generic list for holding the lines
Dim lines As New List(Of String)
'Random class to generate our random number
Dim rnd As New Random()
'Variable to hold our random line number
Dim line As Integer
'Now we loop through each line of our text file
'adding each line to our list
While ioFile.Peek <> -1
lines.Add(ioFile.ReadLine())
End While
'Now we need a random number
line = rnd.Next(lines.Count + 1)
'Now write out the random line to the TextBox
Label1.AppendText(lines(line).Trim())
'Close our StreamReader
ioFile.Close()
'Dispose of the instance
ioFile.Dispose()
ioFile.Close()