Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2010 Open a text file, read a random line, add to listbox, rinse, repeat X times

$
0
0
Hello, this is an old program I am building to read a text file of names, randomize them, and add them to a listbox. I've got it randomizing things properly,but it's not quite working properly. It reads a few names from the file, and then fills the rest with blank space. There are 24 entries in the text file, and it does add 24 entries to the listobx, but most of them are blank spaces. What I think I need to do is get a random number, read the next line of the file that number of times, put that into the listbox, and then reset the position to the top, get another random number, do it again. I feel like my code should work, but doesn't for some reason. I read over this thread, but i can't quite tell what's going on there. This is probably a pretty simple question or just something I've overlooked.

Code:

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim x As Integer = 0
        Dim sr As StreamReader = New StreamReader(txtfile.Text)
        Do While sr.Peek() >= 0
            sr.ReadLine()
            x = x + 1
        Loop
        sr.Close()
        txtnumber.Text = ("Wow! " & x & " people!")
        Dim y As Integer = x
        Dim v As Integer
        Dim name As StreamReader = New StreamReader(txtfile.Text)
        Dim s As String
        Dim t As Integer = 0
        Do While y > 1
            Dim z As System.Random = New System.Random()
            v = z.Next(1, x) 'Get a new random number, call it V
            Do While v > 0

                s = (name.ReadLine())
                v = v - 1
               
            Loop
            ListBox1.Items.Add(New String(s))
            t = t + 1
            Label2.Text = (t)
            TextBox1.Text = (s)

            y = y - 1
        Loop

    End Sub


Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>