Fill Random?
Hi. I want to fill textBox1, textBox2 and textBox3 when I click button1 .. (with random letters and specified length like 5-letter word) what code should I write in button1 click event? I can't find good information about that.. but I think I have to use string. right?
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Sarah Reynolds20 pointsPosted Jan 27, 2012, 4:01 PM EST
{
//Initiate objects & vars
Random random = new Random();
String randomString = "";
int randText;
//Loop 'length' times to generate a random number or character
for (int i = 0; i < length; i++)
{
random.Next(1, 3);
randText = random.Next(97, 123); //char {a-z}
//append random char to random string
randomString = randomString + (char)randText;
}
//return the random string
return randomString;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = generateRandomString(5); //or however long you want the string
}
Hope it helps,
Sarah.
1 short reply
Csharp Ninja60 pointsPosted Feb 1, 2012, 2:14 PM EST