2014-02-06 4 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

당신은 minecraft에서 동일한 steve입니까? : P –

+1

@JohnOdom nope 만약 그가 당신을 안다면 나는 아들에게 물을 수 있습니다 :-) – Steve

+0

스티브 감사합니다, 이것은 짧지 만 'code'에있는 텍스트입니다. c.Text = RandomClass.Next (1,49) .ToString() is 나에게 오류가 발생했습니다. 나는 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 예, 씨앗을 가지고 질문에 과로를 썼습니다. 그 외에 내 대답은 좋은 아이디어를 제공합니다. 임의의 숫자로 채워질 필요가있는 양식의 텍스트 상자를 여러 개 보유하는 방법. 고정 된 수의 텍스트 상자에 고정 된 수의 텍스트 상자뿐만 아니라 –

관련 문제