Click to See Complete Forum and Search --> : Getting random words from text file or string.
anthonywalker
January 8th, 2010, 08:28 AM
Hi Guys,
I am using VB2010 and currently have a random letter and number applicaation but for some reason students dont seem to have the ability to type these in correctly!
Does anyone have some code to put words from text file or string into a text box.
i currently have:
'----------------------------
Dim verbs As String() = {"sin", "beg", "stays", "wash", "clean" , "cups", "gate", "wood"}
'----------------------------
How do i put one of these to a textbox?
tb_word.text = ??
any help would be great thank you!
Anthony
anthonywalker
January 8th, 2010, 08:43 AM
Hi I now have the below but does not seem to work?
-------------------
Dim str As String() = {"sin", "beg", "stays", "wash", "clean"}
Dim N As Integer = str.Length
Dim rnd As New Random((Now.Hour * 3600 + Now.Minute * 60 +
Now.Second) * 1000 + Now.Millisecond)
Dim sb As New StringBuilder
For l As Integer = 1 To len
sb.Append(str(rnd.Next(0, N), 1))
Next
Return sb.ToString
----------------------------
i get the error:
Number of indices exceeds the number of dimensions of the indexed array.
Thanks
Anthony
Oblio
January 8th, 2010, 10:32 AM
Dim rndm As New Random 'place at Class level
Dim verbs As String() = {"sin", "beg", "stays", "wash", "clean", "cups", "gate", "wood"}
Dim selWD As String
selWD = verbs(rndm.Next(0, verbs.Length))
ddclondon
January 15th, 2010, 12:29 AM
Hi Tony
Here's something for you to think about!
I have extrapolated the code more than necessary for ease of reading / understanding.
Block copy the following and place it within a Button_Click event:
Dim intWordLenth As Integer
Dim intWordNumber As Integer
Dim intMaxWords As Integer
Dim intLowerBound As Integer
Dim strSource As String
Dim strResult As String
' Setup the word options
strSource = "JanFebMarAprMayJunJulAugSepOctNovDec"
' Ensure that all words are padded with spaces to make them the same 'length'
intWordLenth = 3
' The total number of words in your source string
intMaxWords = 12
' Set the Lower Bound for the Random Number Generator Range
intLowerBound = 1
' Initialize the random-number generator.
Randomize()
'Get a valid random number between 1 and intMaxWords
intWordNumber = Int((intMaxWords - intLowerBound + 1) * Rnd() + 1)
' Extract the corresponding word
strResult = Mid$(strSource, ((intWordNumber - 1) * intWordLenth + 1), intWordLenth)
lblMonth.Text = strResult
Enjoy!
Oblio
January 15th, 2010, 07:41 AM
Dim rndm As New Random 'place at Class level
Dim verbs() As String = New String() {"sin", "beg", "stays", "wash", "clean", "cups", "gate", "wood"}
Dim selWD As String
selWD = verbs(rndm.Next(0, verbs.Length))
@ddclondon
Dim strSource As String = "JanFebMarAprMayJunJulAugSepOctNovDec"
selWD = strSource.Substring(rndm.Next(0, 12) * 3, 3)
dglienna
January 15th, 2010, 07:46 PM
Dim strSource As String = "JanFebMarAprMayJunJulAugSepOctNovDec"
selWD = strSource.Substring(rndm.Next(0, 12) * 3, 3)
Don't you HATE it when that happens. (catch 'ya next time)
Oblio
January 16th, 2010, 08:38 AM
Don't you HATE it when that happens. (catch 'ya next time)
Do you post at VBForums?
ddclondon
January 21st, 2010, 02:28 AM
Don't you HATE it when that happens.
Absolutely not.
We're in a forum where we can exchange ideas.
Some suggestions are more elegant than others of course.
HanneSThEGreaT
January 27th, 2010, 06:01 AM
We're in a forum where we can exchange ideas.
Some suggestions are more elegant than others of course.
Well Said! :thumb:
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.