Skip to content

Fill Random?

Csharp Ninja

Asked Jan 27, 2012, 2:13 PM EST2 answers
Share
LinkedInX
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.

  • 0
    public String generateRandomString(int length)
     {
    //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.
    0
  • 1 short reply
    • 0
      Thank you sarah
      0