2016-03-21 1 views
2

現在のところ私のプログラムは完璧に動作しますが、私の唯一の苦情は5行のコードを持つこれらの巨大switch文です。それは固いと思われ、読むのは難しいです。しかし私はswitch文のために50行のコードをスクロールさせたくありません。C#ボタン、チェックボックス、ラベルで変数のリストを行う簡単な方法はありますか

私のボタン、チェックボックス、またはラベルにラベルを付けると、配列のようなものを現在の番号にすることができれば不思議でした。チェックボックス1のように、チェックボックス2はチェックボックス[1]とチェックボックス[2]になります。これを行うと動作しませんので、私はこれに対する回避策を探しています。できるだけforループを使用したいのですが、同じことを10回書くのは非常に時間がかかり、時間がかかります。

以下は、私のswitch文の様子です。

switch (currentProblem){ 
    case 1: problem1.Text = (num1 + sign + num2).ToString(); break; 
    case 2: problem2.Text = (num1 + sign + num2).ToString(); problem2.Visible = true; c2.Visible = true; answer2.Visible = true; break; 
    case 3: problem3.Text = (num1 + sign + num2).ToString(); problem3.Visible = true; c3.Visible = true; answer3.Visible = true; break; 
    case 4: problem4.Text = (num1 + sign + num2).ToString(); problem4.Visible = true; c4.Visible = true; answer4.Visible = true; break; 
    case 5: problem5.Text = (num1 + sign + num2).ToString(); problem5.Visible = true; c5.Visible = true; answer5.Visible = true; break; 
    case 6: problem6.Text = (num1 + sign + num2).ToString(); problem6.Visible = true; c6.Visible = true; answer6.Visible = true; break; 
    case 7: problem7.Text = (num1 + sign + num2).ToString(); problem7.Visible = true; c7.Visible = true; answer7.Visible = true; break; 
    case 8: problem8.Text = (num1 + sign + num2).ToString(); problem8.Visible = true; c8.Visible = true; answer8.Visible = true; break; 
    case 9: problem9.Text = (num1 + sign + num2).ToString(); problem9.Visible = true; c9.Visible = true; answer9.Visible = true; break; 
    case 10: problem10.Text = (num1 + sign + num2).ToString(); problem10.Visible = true; c10.Visible = true; answer10.Visible = true; break; 
} 
switch (hiddenCurrentLabel.Text) 
{ 
    case "1": if (answer1.Text != "") { if (answer1.Text == hiddenAnswerLabel.Text) { c1.Checked = true; } addOne(); } else { break; } t = int.Parse(hiddenCurrentLabel.Text); chooseRandoms(t); return; 
    case "2": if (answer2.Text != "") { if (answer2.Text == hiddenAnswerLabel.Text) { c2.Checked = true; } addOne(); } else { break; } t = int.Parse(hiddenCurrentLabel.Text); chooseRandoms(t); return; 
    case "3": if (answer3.Text != "") { if (answer3.Text == hiddenAnswerLabel.Text) { c3.Checked = true; } addOne(); } else { break; } t = int.Parse(hiddenCurrentLabel.Text); chooseRandoms(t); break; 
    case "4": if (answer4.Text != "") { if (answer4.Text == hiddenAnswerLabel.Text) { c4.Checked = true; } addOne(); } else { break; } t = int.Parse(hiddenCurrentLabel.Text); chooseRandoms(t); break; 
    case "5": if (answer5.Text != "") { if (answer5.Text == hiddenAnswerLabel.Text) { c5.Checked = true; } addOne(); } else { break; } t = int.Parse(hiddenCurrentLabel.Text); chooseRandoms(t); break; 
    case "6": if (answer6.Text != "") { if (answer6.Text == hiddenAnswerLabel.Text) { c6.Checked = true; } addOne(); } else { break; } t = int.Parse(hiddenCurrentLabel.Text); chooseRandoms(t); break; 
    case "7": if (answer7.Text != "") { if (answer7.Text == hiddenAnswerLabel.Text) { c7.Checked = true; } addOne(); } else { break; } t = int.Parse(hiddenCurrentLabel.Text); chooseRandoms(t); break; 
    case "8": if (answer8.Text != "") { if (answer8.Text == hiddenAnswerLabel.Text) { c8.Checked = true; } addOne(); } else { break; } t = int.Parse(hiddenCurrentLabel.Text); chooseRandoms(t); break; 
    case "9": if (answer9.Text != "") { if (answer9.Text == hiddenAnswerLabel.Text) { c9.Checked = true; } addOne(); } else { break; } t = int.Parse(hiddenCurrentLabel.Text); chooseRandoms(t); break; 
    case "10": if (answer10.Text != "") { if (answer10.Text == hiddenAnswerLabel.Text) { c10.Checked = true; } getAverage(); } else { break; } t = int.Parse(hiddenCurrentLabel.Text); break; 
} 

第2のものは、forループでそれを持つことで多くの時間を節約することができます。

+0

[関連または重複](http://stackoverflow.com/q/36045587/を993547)。 –

+0

おかげでパトリック、私はそれを試さなければならないでしょう。正確には私が考えていたものではありませんが、もしうまくいくなら、それは機能します。 ;) –

+0

解決策は同じです:配列を繰り返し処理し、何かをしてください。あなたの場合、 'if'ステートメントがそうであるかもしれません。 –

答えて

1
public void ActivateCurrentProblem(int i){ 
      Textbox problem = Controls.Find("problem" + i, true); 
      Textbox answer = Controls.Find("answer" + i, true); 
      CheckBox c = Controls.Find("c" + i, true); 
      problem.Text =(num1 + sign + num2).ToString(); 
      problem.Visible=true; 
      answer.Visible=true; 
     if (answer.Text == hiddenAnswerLabel.Text) 
     { 
      c.Checked = true; 
      addOne(); 
     } 
     t = int.Parse(hiddenCurrentLabel.Text); chooseRandoms(t); 
    } 

と使用方法:

ActivateCurrentProblem(3); 

それとも、単にアレイを使用することができます

Textbox[] txt = new Textbox[10]; 
for(int i=0;i<10;i++){ 
    txt[i] = new Textbox(){ 
     Location =new Point(0, i*40), //values are just examples 
     Visible= true, 
     .... 
    }; 
    Controls.Add(txt[i]); 
} 
+2

素晴らしい解決策です。 - これが機能するには、コントロールに 'Name'プロパティが設定されている必要があります。デザイナーはデフォルトでそれを行いますが、動的に作成された場合、それを期待値に設定する責任があります。 – TaW

+0

...これが私がこれ以上の配列を好む理由です。 –

+0

これを正しく読んでいれば、私のテキストボックスの変数名を "問題" + iと比較しています。私が1だった場合、問題は問題1と見なされるか、または私の理解には欠陥がありますか?それが事実なら、もっと短くて複雑な(私にとっては)暫定的な配列を作る方法かもしれない。私は '{problem1、problem2、problem3、problem4、problem5、problem6、problem7、problem8、problem9、problem10};をタイプアウトする必要はないので、変数名を作成するには –

関連する問題