【问题标题】:Change control colour, and change it back to original更改控制颜色,并将其更改回原始颜色
【发布时间】:2015-07-21 05:48:28
【问题描述】:

我想暂时更改按钮的颜色以显示该按钮已被按下。如何在 C# 中实现这一点?我可以像这样轻松地更改背景颜色:

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

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

【问题讨论】:

标签: c# winforms button background click


【解决方案1】:
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;
}

【讨论】:

    【解决方案2】:
    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;
    

    【讨论】:

      猜你喜欢
      • 2013-06-25
      • 2013-04-12
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      相关资源
      最近更新 更多