On occasion I get an error message popup saying "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: Index."
Here's my code:
Dim ioFile As New System.IO.StreamReader("C:\\testfile.txt") Dim lines As New List(Of String) Dim rnd As New Random() Dim line As Integer While ioFile.Peek <> -1 lines.Add(ioFile.ReadLine()) End While line = rnd.Next(lines.Count + 1) RichTextBox1.AppendText(lines(line).Trim()) ioFile.Close() ioFile.Dispose()
The last bit is where the random number is generated, which I am guessing where the problem itself is, but am unsure as to how to fix it...Any ideas?