有没有一种更简单的方法可以在c#按钮、复选框和标签中列出变量

有没有一种更简单的方法可以在c#按钮、复选框和标签中列出变量,c#,C#,目前,我的程序工作得完美无缺,我唯一的抱怨是它在一行代码中有5行巨大的switch语句。它看起来俗气,而且很难阅读。但是我不想在switch语句中滚动50行代码 我想知道,如果我标记了我的按钮、复选框或标签,我是否可以做一些类似数组的事情来作为当前的数字;与checkbox1类似,checkbox2将是复选框[1]和复选框[2]。现在这样做是行不通的,所以我正在寻找解决办法。如果可能的话,我真的很想使用for循环,因为写出同样的东西10次是非常冗长的,而且比我希望的要耗时得多 下面是我的swit

目前,我的程序工作得完美无缺,我唯一的抱怨是它在一行代码中有5行巨大的switch语句。它看起来俗气,而且很难阅读。但是我不想在switch语句中滚动50行代码

我想知道,如果我标记了我的按钮、复选框或标签,我是否可以做一些类似数组的事情来作为当前的数字;与checkbox1类似,checkbox2将是复选框[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;
}
第二个是在for循环中使用它可以节省大量时间

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]);
}
Textbox[]txt=新文本框[10];

对于(int i=0;i.谢谢patrick,我得试试。这不完全是我所想的,但如果它能工作,它也能工作。;)解决方案是一样的:迭代数组并用它做一些事情。在您的情况下,
if
语句可能适用。请使用数组或控件列表。这样你就可以访问这样的控件:
textboxs[i]=..
现在我唯一关心的是,如果我运行这个for循环,它会停止程序直到它完成吗?或者我最好将它设置为一个do-while循环,这样我可以在需要时取消它?一个很好的解决方案。-请注意,要使其工作,控件必须设置其
Name
属性。默认情况下,设计者会这样做,但如果它们是动态创建的,则编码器有责任将其设置为预期值。。!。。。这就是为什么我更喜欢数组而不是数组。如果我正确地阅读了它,它会将文本框的变量名与“problem”+I进行比较。如果我是1岁,它会将问题视为问题1,还是我的理解有缺陷?如果是这样的话,这可能是一种更短但更复杂的(对我来说)制作临时数组的方法。这样我就不必输入
{problem1,problem2,problem3,problem4,problem5,problem6,problem7,problem8,problem9,problem10}
以创建变量names@PatrickHofman我也喜欢数组。但是问题表明用户没有使用数组。那么,为什么他不能使用它呢?