多个按钮背景图像更改的单一方法?

Single method for multiple button backgroind image changes?

我是新来的。如果你能帮我写一个可以改变被点击按钮的背景图像的方法,我会很高兴。

这就是我现在正在做的事情。正如你所看到的,我正在为每个按钮做这件事。我很想知道是否有一种方法可以执行按钮只会调用的一种方法。

这是一款简单的 Tic Tac Toe 多人游戏。

private void cell23_Click(object sender, EventArgs e)
    {
        if (gameGrid[1, 2] == "n")
        {
            if (currentPlayer == "x")
            {
                this.cell23.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.cross));
                currentPlayer = "o";
                gameGrid[1, 2] = "x";
                cell23.Enabled = false;
            }
            else if (currentPlayer == "o")
            {
                this.cell23.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.nought));
                currentPlayer = "x";
                gameGrid[1, 2] = "o";
                cell23.Enabled = false;
            }

        }

编辑:

private void AnyCell_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        switch (btn.Tag)
        {
            case "cell11":
                ButtonPressed(0,0); break;
            case "two":
            case "three":

            default: break;
        }
    }

现在有了这个方法。当我按下带有标签 cell11 的按钮以获取此代码时,我该如何制作?

使用 sender 参数,它(通常)设置为引发事件的对象。

private void AnyCell_Click(object sender, EventArgs e)
{
    var clickedCell = sender as Control; //Or whatever type it is

    if (gameGrid[1, 2] == "n")
    {
        if (currentPlayer == "x")
        {
            clickedCell.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.cross));
            currentPlayer = "o";
            gameGrid[1, 2] = "x";
            clickedCell.Enabled = false;
        }
        else if (currentPlayer == "o")
        {
            clickedCell.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.nought));
            currentPlayer = "x";
            gameGrid[1, 2] = "o";
            clickedCell.Enabled = false;
        }
    }