954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Word Randomiser

Hi clever people,

I need some help making a VB application which chooses a random line for a file, sets it as the value of a Label (word) and also adds it to a ListBox (list) when a Button (nextbut) is pressed. I also want it to keep going.

Button; Text: Next
Name: nextbut

Label; Name: word

Listbox;Name: list

What i need it to do is to load a line from a file ("C:\words.txt" for example) and set it as the value f the label. The lines are different lenghts.

Any hwlp would be highly appreciated

butler1233
Newbie Poster
7 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

You can get the list of words into an array by

Dim words() As String = Replace(My.Computer.FileSystem.ReadAllText(filename), vbLf, "").Split(vbCr)

To generate a random index for words you can call

Private Function Random(ByVal lo, ByVal hi)

    'returns a random integer in the range [lo,hi]

    Random = lo + CInt(Rnd() * (hi - lo))

End Function

Just pass it lo=0 and hi=ubound(words). Make sure you call Randomize() when the form loads to avoid generating the same string of random numbers each run.

Reverend Jim
Posting Shark
Moderator
1,169 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: