PDA

Click to See Complete Forum and Search --> : Reading values from a text file


scottr
Jan 12th, 2000, 05:40 AM
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

X_Darknight_X
Jan 12th, 2000, 06:04 AM
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 (http://www.icq.com/14028049)
E-mail - darknight23@hotmail.com

scottr
Jan 13th, 2000, 11:40 AM
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?

scottr
Jan 13th, 2000, 12:07 PM
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.