Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#缩短指定给重置值的按钮_C#_Wpf_Visual Studio 2012 - Fatal编程技术网

C#缩短指定给重置值的按钮

C#缩短指定给重置值的按钮,c#,wpf,visual-studio-2012,C#,Wpf,Visual Studio 2012,我刚开始编程意味着我是新来的。。。我用else-if语句做了一个tic-tac-toe游戏 下面的代码是将在我的游戏中重置某些值的代码,我肯定有,但我找不到答案-如何缩短这段代码 btn1.Enabled = true; btn2.Enabled = true; btn3.Enabled = true; btn4.Enabled = true; btn5.Enabled = true; btn6.En

我刚开始编程意味着我是新来的。。。我用else-if语句做了一个tic-tac-toe游戏

下面的代码是将在我的游戏中重置某些值的代码,我肯定有,但我找不到答案-如何缩短这段代码

        btn1.Enabled = true;
        btn2.Enabled = true;
        btn3.Enabled = true;
        btn4.Enabled = true;
        btn5.Enabled = true;
        btn6.Enabled = true;
        btn7.Enabled = true;
        btn8.Enabled = true;
        btn9.Enabled = true;

        btn1.Text = "";
        btn2.Text = "";
        btn3.Text = "";
        btn4.Text = "";
        btn5.Text = "";
        btn6.Text = "";
        btn7.Text = "";
        btn8.Text = "";
        btn9.Text = "";

        btn1.BackColor = default(Color);
        btn2.BackColor = default(Color);
        btn3.BackColor = default(Color);
        btn4.BackColor = default(Color);
        btn5.BackColor = default(Color);
        btn6.BackColor = default(Color);
        btn7.BackColor = default(Color);
        btn8.BackColor = default(Color);
        btn9.BackColor = default(Color);

您应该使用一个,或者更好的是,一个可以容纳所有按钮

例如:

 List<Button> buttonList = new List<Button>();

 // Add buttons to list...
 buttonList.Add(btn1);
 buttonList.Add(btn2);
 ...

上面的代码将在列表中的所有按钮上运行,并为每个按钮清除文本并设置背景色。

您应该使用一个,或者更好的是,使用一个来保存所有按钮

例如:

 List<Button> buttonList = new List<Button>();

 // Add buttons to list...
 buttonList.Add(btn1);
 buttonList.Add(btn2);
 ...

上面的代码将在列表中的所有按钮上运行,并为每个按钮清除文本并设置背景色。

这应该使用数组来完成,而不是btn1,您将使用btn[],然后您可以使用for循环访问它们:

for (i=0; i<9; i++) {
btn[i].Enabled = true;
btn[i].Text = "";
btn[i].Color = default(Color);
}

for(i=0;i这应该用数组来完成,而不是用btn1,你应该有btn[],然后你可以用for循环来访问它们:

for (i=0; i<9; i++) {
btn[i].Enabled = true;
btn[i].Text = "";
btn[i].Color = default(Color);
}

用于(i=0;i重置所有控制按钮

foreach(var item in this.Controls)
{
    if (item is Button)
    {
        item.Enabled = true;
        item.Text = "";
        item.BackColor = default(Color);
    }
}

控制面板上所有按钮的重置

foreach(var item in this.Controls)
{
    if (item is Button)
    {
        item.Enabled = true;
        item.Text = "";
        item.BackColor = default(Color);
    }
}