Results 1 to 4 of 4

Thread: Reading values from a text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 1999
    Posts
    89

    Post

    Hi all,

    I am programing a small hangman game in vb. Right now i have the word list hardcoded into the applicaion. what i would like to do is have the app read a txt file and take the words from that. My question is how to i get vb to read a txt file. I dont want to use a db cause i think that would be overkill. Any suggestions would be welcome and appreciated

    Scott

  2. #2
    Member
    Join Date
    Jul 1999
    Location
    J-ville, NC
    Posts
    54

    Post

    Try doing this:

    Make a listbox (List1) and hide somewhere on the form. To create the text file, make sure you create a vertical list with quotations around each word.

    (For example)

    "North Carolina"
    "Alabama"
    "New York"
    "California"

    Now in the Form Load put this:

    (This will add the words to the listbox)

    Dim Item$
    Open "C:\Location of textfile" For Input as #1
    Do While Not EOF(1)
    Input #1, Item$
    List1.AddItem Item$
    Loop
    Close #1

    To read from the Listbox to see if the user has spelled on of the words or whatever you do....

    Dim x%
    For x% = List1.ListCount - 1
    If Text1.Text = List1.List(x%) then
    Msgbox("You WIN!")
    End If
    Next x%

    If you have any other questions, reply back

    ------------------
    David Underwood
    Teen Programmer

    ICQ - 14028049
    E-mail - [email protected]



  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 1999
    Posts
    89

    Post

    I guess my next question would be the proper way to randomize the selection from the list box. Would you have to create an array from the listbox with the values and then perform the randomize function?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 1999
    Posts
    89

    Post

    I feel bad but i think i have the answer to my own question

    Dim intrandom as integer
    dim word as string

    Randomize
    intrandom = Int(list1.ListCount - 1) * Rnd

    word = list1.List(intrandom)

    this seems to work, if you have a better way please feel free to write it in.

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