简体   繁体   English

计时器(winforms)

[英]Timer (winforms)

i want timer to run a counter from 0 to 3. i added the timer from the toolbox in visual basics 2008 (instead of creating an object and using properties) you can see the Timer at the bottom of the winform.. 我希望计时器从0到3运行一个计数器。我在Visual Basics 2008中从工具箱添加了计时器(而不是创建对象和使用属性),您可以在winform的底部看到计时器。

        int timerCounter;
    private void animationTimer_Tick(object sender, EventArgs e)
    {
        //timer should go 0,1,2,3..and then reset
        while (true)
        {
            timerCounter++;
            if (timerCounter > 3)
            {
                timerCounter = 0;
            }
            game.Twinkle();
            //screen gets repainted.
            Refresh();
        }



    }

will the timer work? 计时器会工作吗? (i enabled it and set it to 33 milliseconds) (我启用了它并将其设置为33毫秒)

Set the timer's interval to 1000 (which is 1000ms or 1 second). 将计时器的时间间隔设置为1000(即1000ms或1秒)。 Then when you enable it, it'll go on and on and on, firing the timer1_tick event every time it goes through the interval. 然后,当您启用它时,它将不断打开,并在每次经过该间隔时触发timer1_tick事件。

Here's an example on how to do it: 这是有关如何执行此操作的示例:

int count = 0;
private void timer1_Tick(object sender, EventArgs e)
{
    count++;

    if (count == 3)
    {
        //Do something here, because it's the third toll of the bell.

        //But also reset the counter after you're done.
        count = 0;
    }
}

Don't forget to .Enable the timer! 别忘了启用计时器!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM