Get amount of lines, randomize, then read a certain line.

decrypt

Well-known member
Joined
Oct 6, 2003
Messages
216
I want to make a program that has a file called "quotes.txt", with a different quote on every line. When the user clicks a button, it will get the amount of lines in the "quotes.txt", randomize a number in between 1 and the number of lines. And then go to the line of the number it just randomized. So for example:

Number of lines in "quote.txt" = 10
Randomize a number between 1 and 10
number is 4
Go to line 4 of "quote.txt"
Display line 4 of "quote.txt" in a textbox

Is it possible to do all of this? Im not sure how to do any of this. Could someone make up the code for this, because im not really good with reading from a text file...

thanks...
 
Hi there,

I dont really understand what your trying to do, but you could try something like this:
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim MyLines As New ArrayList()
        Dim Total As Integer
        Dim Random As Integer
        Dim sreader As New IO.StreamReader("C:/quotes.txt")
        While Not sreader.Peek
            MyLines.Add(sreader.ReadLine)
        End While
        sreader.Close()
        Total = MyLines.Count
        Random = New System.Random().Next(0, Total)
        TextBox1.Text = MyLines(Random)
        MyLines.Clear()
    End Sub
 
Back
Top