[Resolved] Showing random information in windows application
Another problem surprise surprise!
This time I want to get information from a text file and have it displayed in a windows application. I want to have a different piece of info on each line in the text file and the information shown will be chosen randomly each time the program is run. So far I have got the information to show up in the windows application but it is only reading the first line I dunno how to make it choose randomly. Any suggestions? Thanks.
Re: Showing random information in windows application
VB Code:
Dim txtIN As IO.TextReader
Dim str As String
Dim arr As New ArrayList
Dim rnd As New System.Random
Try
txtIN = IO.File.OpenText("C:\Random.txt")
While txtIN.Peek <> -1
arr.Add(txtIN.ReadLine)
End While
Me.Label1.Text = arr.Item(rnd.Next(0, arr.Count)).ToString
Catch ex As Exception
'Handle
Finally
If Not txtIN Is Nothing Then
txtIN.Close()
End If
End Try
Re: Showing random information in windows application
If this is 2005:
VB Code:
Dim lines As String() = My.Computer.FileSystem.ReadAllText("file path here").Split(New String() {Environment.NewLine}, _
StringSplitOptions.RemoveEmptyEntries)
MessageBox.Show(lines((New Random).Next(0, lines.Length)))
If you need to reuse the Random object then create it before the call to MessageBox.Show and assign it to a variable.
Re: Showing random information in windows application
Ok, this has been resolved. Thanks a lot jmcilhinney