更改控件颜色,并将其更改回原始颜色

时间:2015-05-10 19:08:29

标签: c# winforms button background click

我想暂时改变按钮的颜色以显示按钮已被按下。我怎样才能在C#中实现这一目标?我可以像这样轻松地改变背景颜色:

private void button1_Click(object sender, EventArgs e)
{
    button1.BackColor = Color.Green;    
}
按钮的

,但如何在延迟后恢复这些更改?

2 个答案:

答案 0 :(得分:0)

private void button1_Click(object sender, EventArgs e)
{
    button1.BackColor = Color.Azure;
    var aTimer = new System.Timers.Timer(2000);
    aTimer.Elapsed += OnTimedEvent;
    aTimer.Enabled = true;
}

private  void OnTimedEvent(Object source, ElapsedEventArgs e)
{
    button1.BackColor =  SystemColors.Control;
}

答案 1 :(得分:0)

Color btnBackColor;
Timer timer = new Timer();

private void button1_Click(object sender, EventArgs e)
{
    btnBackColor = button1.backColor;
    button1.BackColor = Color.Green;    
    timer.Enabled = true;
}

private void timer_Tick(object sender, EventArgs e)
{
    button1.BackColor = btnBackColor;
    timer.Enabled=false;
}

并将以下行添加到构造函数中:

    timer.Tick += timer_Tick;
    timer.Elapsed = 2000;