Random text generator
Hi,
I'm very new to VB Express, I'm taking a college class about it. For a project, I would like to display a set of random statements when the user clicks a button, like a fortune cookie program. So when the user clicks btnSaying, I want one of eight statements to display. It is probably far more simple than I think, but for some reason I can't figure out how to set this up. I've read all kinds of different posts about random numbers and letters and arrays and things like that. I think I might just be lost. Please help me.
sure.
Basically you need a way to store your "statements", for simple reasons lets stick with an ArrayList.
Dim theStatements as new ArrayList()
declare that at a global level so other methods/functions in your class can access it.
Next up, at some stage you will be populating/filling the array with your statements. To do say, say if we had a button called "Fill statements", then in this double click event we would do something like this:
Me.theStatements.Clear()
Me.theStatements.Add("Statement 1")
Me.theStatements.Add("Statement 2")
..
..
Next up, the final part, when the user click on the main button to show your random statement, simply do this on the double click event of that button (Double click it on the designer view to create the event)
Dim theRandomNumber as new Random()
Dim theNumberChosen as Integer = theRandomNumber.Next(0, Me.theStatements.Count)
MessageBox.Show(Me.theStatements(theNumberChosen).ToString())
this will:
create a random class instanceget a random number between 0 and the number of items in the arraylistshow you the message, at the specified location int the array.does this help?
Actually you did not provide a seed so the random class will produce the same sequence of
of numbers.
seeding can be produced thusly:
Dim
r As New Random(Now.Ticks And &HFFFFFFF)Dim num As Short = r.Next(0, 8)simply declare it at the top of the class. Example:
public class MyClass
private myArray as new ArrayList()
..
..
private sub MyFunction()
Me.myArray.Add("hi!") 'adds item into the global array variable
end sub
end class