简体   繁体   English

在按下另一个按钮后重置先前按下的按钮的颜色c#

[英]reset the color of a previously pressed button after another button is pressed c#

I'm using C# windows form application, and i'm facing a problem resetting a button's back Color. 我正在使用C#Windows窗体应用程序,并且遇到重置按钮背面颜色的问题。

By clicking a button I need it to change its back color and reset the back color of previously pressed button. 通过单击按钮,我需要它来更改其背景色并重置先前按下的按钮的背景色。

Please note that i have lots of buttons in the form and i'm using "sender" to apply same clicking event to all buttons. 请注意,我在表单中有很多按钮,并且我使用“发件人”将相同的单击事件应用于所有按钮。

You'll need to keep track of the last button that was clicked. 您需要跟踪单击的最后一个按钮。 Add a private field and then manipulate it in the on click event handler: 添加一个私有字段,然后在on click事件处理程序中对其进行操作:

class Form1 : Form
{
    private Button _lastButtonClicked;

    protected void ClickHandler(object sender, EventArgs e)
    {
        if (_lastButtonClicked != null)
           _lastButtonClicked.BackColor = Color.whatever;

        _lastButtonClicked = sender as Button;
        _lastButtonClicked = Color.newcolor;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM