c#突出显示数字较小的代码片段

时间:2015-03-24 19:06:38

标签: c#

我正在创建一个宾果游戏机,我有代码,所以一旦按下它将检查已调用的数字并突出显示从宾果游戏机调用的那些数字。

我想知道是否有更好的方法来限制代码大小?而不是单独列出所有?这有助于提高速度并减少程序大小。

谢谢。

private void btnCheckNumbs_Click(object sender, EventArgs e)
    {
        for (int i = 1; i <= Globals.NextBalls; i++)
        {
            if (Globals.balls[i] == 1) txtBoxs1.BackColor = Color.Aqua;
            if (Globals.balls[i] == 2) txtBoxs2.BackColor = Color.Aqua;
            if (Globals.balls[i] == 3) txtBoxs3.BackColor = Color.Aqua;
            if (Globals.balls[i] == 4) txtBoxs4.BackColor = Color.Aqua;
            if (Globals.balls[i] == 5) txtBoxs5.BackColor = Color.Aqua;
            if (Globals.balls[i] == 6) txtBoxs6.BackColor = Color.Aqua;
            if (Globals.balls[i] == 7) txtBoxs7.BackColor = Color.Aqua;
            if (Globals.balls[i] == 8) txtBoxs8.BackColor = Color.Aqua;
            if (Globals.balls[i] == 9) txtBoxs9.BackColor = Color.Aqua;
            if (Globals.balls[i] == 10) txtBoxs10.BackColor = Color.Aqua;

            if (Globals.balls[i] == 11) txtBoxs11.BackColor = Color.Aqua;
            if (Globals.balls[i] == 12) txtBoxs12.BackColor = Color.Aqua;
            if (Globals.balls[i] == 13) txtBoxs13.BackColor = Color.Aqua;
            if (Globals.balls[i] == 14) txtBoxs14.BackColor = Color.Aqua;
            if (Globals.balls[i] == 15) txtBoxs15.BackColor = Color.Aqua;
            if (Globals.balls[i] == 16) txtBoxs16.BackColor = Color.Aqua;
            if (Globals.balls[i] == 17) txtBoxs17.BackColor = Color.Aqua;
            if (Globals.balls[i] == 18) txtBoxs18.BackColor = Color.Aqua;
            if (Globals.balls[i] == 19) txtBoxs19.BackColor = Color.Aqua;
            if (Globals.balls[i] == 20) txtBoxs20.BackColor = Color.Aqua;

声明达到90 ....

1 个答案:

答案 0 :(得分:0)

试试这个

for (int i = 1; i <= Globals.NextBalls; i++)
{

    for (int k = 1; k <= 90; k++)
    {
        if (Globals.balls[i] == k) 
        {
            TextBox txtName =  (TextBox)this.Controls.Find("txtBoxs"+k.ToString(), true)[0];
            txtName.BackColor = Color.Aqua;
        }
    }
}