|
-
May 1st, 2006, 04:00 PM
#1
Thread Starter
New Member
[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.
Last edited by vb_newbie; May 1st, 2006 at 05:09 PM.
-
May 1st, 2006, 04:05 PM
#2
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
-
May 1st, 2006, 04:13 PM
#3
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.
-
May 1st, 2006, 05:06 PM
#4
Thread Starter
New Member
Re: Showing random information in windows application
Ok, this has been resolved. Thanks a lot jmcilhinney
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|