更改按钮的颜色很短的时间 [英] Change button color for a short time

查看:188
本文介绍了更改按钮的颜色很短的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变一个按钮的backgroundColor为几秒钟石灰,然后改回正常。出于某种原因,它只是不工作,我要等待那些秒钟,有些东西我还测试工作,但backgroundColor时没有改变。这是我到目前为止已经试过:

I would like to change the backgroundcolor of a button for a couple of seconds to lime and then change it back to normal. For some reason it simply doesnt work, I have to wait for those seconds and some things I tested also work but the backgroundcolor isn't changed. This is what I've tried so far:

private void button1_Click(object sender, EventArgs e)
{
    button1.BackColor = Color.Lime;
    Thread.Sleep(2000);
    button1.BackColor = SystemColors.Control;
}

希望有人可以帮我这个!

Hopefully someone can help me out with this!

推荐答案

您需要一个计时器。
从工具箱一个计时器添加到您的窗体。多布尔点击它添加一个计时器Tick事件处理程序

You need a timer. Add a Timer from Tool Box to your form. Doble click on it to add a timer tick event handler

    private void button1_Click(object sender, EventArgs e)
    {
        button1.BackColor = Color.Lime;
        //Set time between ticks in miliseconds.
        timer1.Tick=2000;
        //Start timer, your program continues execution normaly
        timer1.Start;
        //If you use sleep(2000) your program stop working for two seconds.

    }
        //this event will rise every time set in Tick (from start to stop)
        private void timer1_Tick(object sender, EventArgs e)
        {
          //When execution reach here, it meas 2 secons have past. Stop Timer and change color
          timer1.Stop;
          button1.BackColor = SystemColors.Control;
        }

这篇关于更改按钮的颜色很短的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆