Visual Studio c#计时器不起作用

时间:2016-09-26 04:04:01

标签: c# visual-studio-2015

单击按钮时,我希望按钮的颜色在5秒后变为黑色,但我无法使其工作。我已经将定时器的间隔设置为5000,并在属性中将Enabled设置为true。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Start();

            button1.BackColor = Color.Black;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }
    }
}

4 个答案:

答案 0 :(得分:2)

如果您想要将颜色更改为黑色,并且在 $.ajax({ type: "POST", url: "pgLoadFile.aspx/checkFilesProcess", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { if (data.d != "" && data.d != undefined) { if (data.d == "0") { $("#showCheckFileFailMessage").show("slow"); } else { $("#showCheckFileSuccessMessage").show("slow"); $("#trLoadFile").show("slow"); } } else $("#showCheckFileFailMessage").show("slow"); }, error: function (ts) { alert(ts.responseText) $("#showCheckFileFailMessage").show("slow"); }, timeout: 60000 }); 之后保持这种状态,则需要将5 seconds的分配放在button1.BackColor事件处理程序中。此外,不要忘记停止计时器滴答作响。

timer1_Tick

答案 1 :(得分:2)

最好的灵魂就是,

 private void button1_Click(object sender, EventArgs e)
        {            
            Timer MyTimer = new Timer();
            MyTimer.Interval = 4000; 
            MyTimer.Tick += new EventHandler(MyTimer_Tick);
            MyTimer.Start();

        }

        private void MyTimer_Tick(object sender, EventArgs e)
        {
            button1.BackColor = Color.Black;

        }

答案 2 :(得分:1)

试试这个:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {

        button1.BackColor = Color.Black;
        timer1.Stop();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;
        timer1.Interval = 5000;
        timer1.Start();


    }
}

你必须将按钮的黑色背景的触发器放在计时器的刻度事件上。

答案 3 :(得分:0)

在timer_tick事件

中写下颜色更改代码
private void timer1_Tick(object sender, EventArgs e)
    {
      button1.BackColor = Color.Black;
      timer1.Enabled = false;
    }