Results 1 to 4 of 4

Thread: [Resolved] Showing random information in windows application

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    9

    [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.

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Showing random information in windows application

    VB Code:
    1. Dim txtIN As IO.TextReader
    2.         Dim str As String
    3.         Dim arr As New ArrayList
    4.         Dim rnd As New System.Random
    5.  
    6.         Try
    7.             txtIN = IO.File.OpenText("C:\Random.txt")
    8.             While txtIN.Peek <> -1
    9.                 arr.Add(txtIN.ReadLine)
    10.             End While
    11.  
    12.             Me.Label1.Text = arr.Item(rnd.Next(0, arr.Count)).ToString
    13.         Catch ex As Exception
    14.             'Handle
    15.         Finally
    16.             If Not txtIN Is Nothing Then
    17.                 txtIN.Close()
    18.             End If
    19.         End Try

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Showing random information in windows application

    If this is 2005:
    VB Code:
    1. Dim lines As String() = My.Computer.FileSystem.ReadAllText("file path here").Split(New String() {Environment.NewLine}, _
    2.                                                                                            StringSplitOptions.RemoveEmptyEntries)
    3.  
    4.         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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    9

    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
  •  



Click Here to Expand Forum to Full Width