仅使用一种方法更改不同的按钮文本

Changing different button text using only one method

C# 相对较新。我必须做一个井字游戏。我正在考虑只使用一种方法来更改我的按钮属性。

这就是我的想象。

int count = 0;
private void button1_Click(object sender, EventArgs e)
{
    ChangeButton(count);
}
public int ChangeButton(int i)
{
    if(count % 2 == 0)
    {
        // button.text = x
        // i want to be able to change the text of whichever button is clicked
    }
    else
    {
        // button.text = o
    }
    // button.enable = false
    // I want to disable whichever button is clicked
    i++;
    return i;
}

我不知道我应该做 // 部分。希望您能够帮助我。谢谢!

如果你有多个按钮都调用同一个点击事件,你可以通过发送参数来识别按钮:

Button btn = sender as Button;

然后您可以更改该按钮的文本:

btn.Text = count++ % 2 == 0 ? "x" : "o";

全部在你的点击事件中:

int count = 0;
private void button1_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    btn.Text = count++ % 2 == 0 ? "x" : "o";
    btn.Enabled = false;
}