user3281606
user3281606

Reputation: 15

Out Put Random Numbers to TextBox

I am new to C#. I have created a random class that manages to generate some not so very random numbers. But my problem is that I want to output these numbers to 6 different text boxes. I'm sure sure there is a more efficient way of doing it which is also less long winded. and this is how I have done it:

protected void genBtn_Click(object sender, EventArgs e)
{

    Random RandomClass = new Random();
    int num1 , num2, num3 , num4, num5 , num6;
    num1 = RandomClass.Next(1,49);
    num2 = RandomClass.Next(1,49);
    num3 = RandomClass.Next(1,49);
    num4 = RandomClass.Next(1,49);
    num5 = RandomClass.Next(1,49);
    num6 = RandomClass.Next(1,49);



    TextBox1.Text = num1.ToString();
    TextBox2.Text = num2.ToString();
    TextBox3.Text = num3.ToString();
    TextBox4.Text = num4.ToString();
    TextBox5.Text = num5.ToString();
    TextBox6.Text = num6.ToString();

Upvotes: 1

Views: 2923

Answers (4)

T.S.
T.S.

Reputation: 19340

You may have your text boxes on different panel controls and you may have more text boxes. Do this:

// Name your random num text boxes with some convention, like txtRand1. But not the others
// Recursive func to read all text boxes
private void RecFillRandTextBoxes(Control parent, rand)
{
    foreach (Control  c in parentcontrols) 
    {
        if (typeof(c) is TextBox && c.Name.StartsWith("txtRand"))
            c.Text = Next(1,49).ToString();
        else
            RecFillRandTextBoxes(c, rand);          
    }
}

Then just call it in your form class on genBtn_Click

RecFillRandTextBoxes(this, rand);

Upvotes: 0

Steve
Steve

Reputation: 216293

Create an array, then loop?

Random RandomClass = new Random();
Control[] textboxes = new Contro[] {TextBox1,TextBox2,TextBox3,TextBox4,TextBox5,TextBox6};
foreach(Control c in textboxes) 
     c.Text = RandomClass.Next(1,49).ToString();

Or a List<TextBox> then ForEach ?

List<TextBox> textboxes = new List<TextBox>() {TextBox1,TextBox2,TextBox3,TextBox4,TextBox5,TextBox6};
textboxes.ForEach(x => x.Text = RandomClass.Next(1,49).ToString());

Upvotes: 2

Mike Schwartz
Mike Schwartz

Reputation: 2212

You could just do this:

protected void genBtn_Click(object sender, EventArgs e)
{

    Random RandomClass = new Random();

        TextBox1.Text = RandomClass.Next(1, 49).ToString();
        TextBox2.Text = RandomClass.Next(1,49).ToString();
        TextBox3.Text = RandomClass.Next(1,49).ToString();
        TextBox4.Text = RandomClass.Next(1,49).ToString();
        TextBox5.Text = RandomClass.Next(1,49).ToString();
        TextBox6.Text = RandomClass.Next(1,49).ToString();
}

Upvotes: 0

Joe Brunscheon
Joe Brunscheon

Reputation: 1989

Your example works, but you don't need all those extra variables.

protected void genBtn_Click(object sender, EventArgs e)
{
    Random RandomClass = new Random();
    TextBox1.Text = RandomClass.Next(1,49).ToString();
    TextBox2.Text = RandomClass.Next(1,49).ToString();
    TextBox3.Text = RandomClass.Next(1,49).ToString();
    TextBox4.Text = RandomClass.Next(1,49).ToString();
    TextBox5.Text = RandomClass.Next(1,49).ToString();
    TextBox6.Text = RandomClass.Next(1,49).ToString();
}

Upvotes: 0

Related Questions