如何根据点击的按钮更改按钮的属性?

时间:2016-06-01 09:33:25

标签: c#

我正在尝试更改已单击按钮的属性。根据点击的按钮,我无法弄清楚如何做到这一点。我不想为每个按钮复制方法。

这是我的代码:

private void GetPlayerChoice_Click(object sender, EventArgs e)
{

    if (playerX.Turn == true)
    {
        button1.Text = playerX.Name;
        button1.BackColor = Color.FromArgb(255, 15, 0);
        playerX.Turn = false;
        playerO.Turn = true;
    }
    if (playerO.Turn == true)
    {
        button1.Text = playerO.Name;
        button1.BackColor = Color.FromArgb(96, 255, 40);
        playerX.Turn = true;
        playerO.Turn = false;
    }
}

1 个答案:

答案 0 :(得分:1)

你在eventhandler的sender参数中有你的按钮。 所以你可以尝试:

Button button = sender as Button;
if (button != null)
{
    button.Text = "Clicked";
}