|
-
January 8th, 2010, 09:28 AM
#1
Getting random words from text file or string.
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
-
January 8th, 2010, 09:43 AM
#2
Re: Getting random words from text file or string.
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
Last edited by anthonywalker; January 8th, 2010 at 10:21 AM.
-
January 8th, 2010, 11:32 AM
#3
Re: Getting random words from text file or string.
Code:
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))
-
January 15th, 2010, 01:29 AM
#4
Re: Getting random words from text file or string.
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!
Last edited by ddclondon; January 22nd, 2010 at 01:54 AM.
-
January 15th, 2010, 08:41 AM
#5
Re: Getting random words from text file or string.
Code:
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
Code:
Dim strSource As String = "JanFebMarAprMayJunJulAugSepOctNovDec"
selWD = strSource.Substring(rndm.Next(0, 12) * 3, 3)
-
January 15th, 2010, 08:46 PM
#6
Re: Getting random words from text file or string.
 Originally Posted by Oblio
Code:
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)
-
January 16th, 2010, 09:38 AM
#7
Re: Getting random words from text file or string.
 Originally Posted by dglienna
Don't you HATE it when that happens. (catch 'ya next time)
Do you post at VBForums?
-
January 21st, 2010, 03:28 AM
#8
Re: Getting random words from text file or string.
 Originally Posted by dglienna
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.
-
January 27th, 2010, 07:01 AM
#9
Re: Getting random words from text file or string.
 Originally Posted by ddclondon
We're in a forum where we can exchange ideas.
Some suggestions are more elegant than others of course.
Well Said!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|