我如何能在C#中此计时器? [英] How do I enable this timer in C#?

查看:95
本文介绍了我如何能在C#中此计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始了C#的课程,我不能让我的定时器运行。它可能很简单,我只是错过了在这里的东西。基本上我有一个按钮来启动和停止红绿灯序列。我想1秒的时间间隔。下面有我写的东西。如预期,当我按下启动它不工作。谢谢

I've started a course of c# and I cant get my timer to run. Its probably pretty simple and I've just missed something here. Basically I have a button to start and stop a traffic light sequence. I wanted an interval of 1 second. Heres what I've written. It doesn't work as intended when I press start. Thank you.

 }
    public int counter = 0;

private void rbStart_CheckedChanged(object sender, EventArgs e)
{

    counter++;

    if (counter == 1)
    {
        pbRed.Visible = true;
        pbAmber.Visible = false;
        pbGreen.Visible = false;
    }
    else if (counter == 2)
    {
        pbRed.Visible = true;
        pbAmber.Visible = true;
        pbGreen.Visible = false;
    }
    else if (counter == 3)
    {
        pbRed.Visible = false;
        pbAmber.Visible = false;
        pbGreen.Visible = true;
    }
    else if (counter == 4)
    {
        pbRed.Visible = false;
        pbAmber.Visible = true;
        pbGreen.Visible = false;
    }
    else if (counter == 5)
    {
        pbRed.Visible = true;
        pbAmber.Visible = false;
        pbGreen.Visible = false;
    }
    else
    {
        counter = 0;
    }
}

private void rbStop_CheckedChanged(object sender, EventArgs e)
{

    pbRed.Visible = false;
    pbAmber.Visible = false;
    pbGreen.Visible = false;
}

private void Form1_Load(object sender, EventArgs e)
{
    Light_timer.Tick += new EventHandler(rbStart_CheckedChanged);
    Light_timer.Interval = 1000;

}



}

}

推荐答案

我想你可能误解了定时器的工作方式。该 Timer.Tick事件火灾时,间隔已过。间隔所使用的定时器来确定多久蜱之间运行。它的值永远不会由定时器改变。事实上,System.Windows.Forms.Timer没有办法找回逝去的时间,这意味着你将需要自己的状态跟踪机制,不依赖于这一点。就拿我在上面所引用的页面上的例子好好看看,并确保你明白它是如何工作的,然后给它一个镜头。

I think you probably misinterpreted how the Timer works. The Timer.Tick Event fires when the Interval has elapsed. Interval is used by the Timer to determine how long to run between ticks. Its value is never changed by the Timer. In fact, a System.Windows.Forms.Timer has no way to retrieve elapsed time, which means you'll need a state tracking mechanism of your own that doesn't depend on that. Take a good look at the example on the page I referenced above and make sure you understand how it works, then give it another shot.

这篇关于我如何能在C#中此计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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