我想暂时改变按钮的颜色以显示按钮已被按下。我怎样才能在C#中实现这一目标?我可以像这样轻松地改变背景颜色:
private void button1_Click(object sender, EventArgs e)
{
button1.BackColor = Color.Green;
}
按钮的,但如何在延迟后恢复这些更改?
答案 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;