C# 如何在按下按钮时更改按钮的背景色,并使用其他按钮将颜色更改为其他颜色
我一直在一个windows电影预订软件中工作,它有大约20个按钮,我无法让我的按钮代码工作。它只在按下时变为(绿色),在按下预订按钮后没有功能将其变为红色C# 如何在按下按钮时更改按钮的背景色,并使用其他按钮将颜色更改为其他颜色,c#,winforms,C#,Winforms,我一直在一个windows电影预订软件中工作,它有大约20个按钮,我无法让我的按钮代码工作。它只在按下时变为(绿色),在按下预订按钮后没有功能将其变为红色 private Button lastButton = null; private void button57_Click(object sender, EventArgs e) { // Change the background color of the button that was clicked Button cu
private Button lastButton = null;
private void button57_Click(object sender, EventArgs e)
{
// Change the background color of the button that was clicked
Button current = (Button)sender;
current.BackColor = Color.GreenYellow;
// Revert the background color of the previously-colored button, if any
if (lastButton != null)
lastButton.BackColor = SystemColors.Control;
// Update the previously-colored button
lastButton = current;
}
连续按同一按钮两次会出现问题,因为上一个按钮和当前按钮是相同的。我会像这样排除这种情况:
if (lastButton != null && lastButton != current) {
lastButton.BackColor = SystemColors.Control;
// Update the previously-colored button
lastButton = current;
}
这里的问题是什么?您的代码应该可以正常工作,当您按下按钮时,它会将背景颜色更改为绿色,并将以前单击的任何按钮的颜色设置回原来的颜色。您的“预订按钮”代码在哪里?