2014-02-06 75 views
1

我是C#的新手。我创建了一个随机类,设法生成一些不是非常随机的数字。但我的问题是我想输出这些数字到6个不同的文本框。我确信有一种更有效的方法可以做到这一点,也不那么长时间。 这是我怎么做它:把随机数字放到文本框中

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(); 
+2

问题是什么/问题? –

回答

2

创建一个数组,然后循环?

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(); 

还是一个List<TextBox>然后ForEach

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

你是来自我的世界的同一个史蒂夫吗? :P –

+1

@JohnOdom nope我可以问我的儿子,如果他知道你:-) – Steve

+0

谢谢史蒂夫,这是很好,但短,但在'代码'文本c.Text = RandomClass.Next(1,49).ToString()是造成我的错误。我认为它与textbox.text中的相同,但错误按摩说我不应该用作类型。我需要在这条线之前的某个地方宣布这个吗? – user3281606

0

你的榜样工作,但你并不需要所有这些额外的变量。

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(); 
} 
0

你可能只是这样做:

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(); 
} 
0

您可能会有不同的面板控制你的文本框,你可能有多个文本框。这样做:

// 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);   
    } 
} 

然后只是把它在你的窗体类上genBtn_Click

RecFillRandTextBoxes(this, rand); 
+0

这没有回答这个问题,并且Random的默认构造函数已经使用了时间依赖的种子:http://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx – GvS

+0

@GvS是的,我用种子过度练习了这个问题。除此之外,我的答案给出了一个很好的想法,即如何在窗体上添加任意数量的文本框,这些文本框需要填充随机数。不只是固定数量的表面上固定数量的文本框 –