【问题标题】:Timer (winforms)计时器(winforms)
【发布时间】:2011-08-02 03:47:14
【问题描述】:

我希望计时器运行一个从 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();
        }



    }

计时器会工作吗? (我启用它并将其设置为 33 毫秒)

【问题讨论】:

  • 几率约为 99%。尝试问一个下次可以可靠回答的问题。
  • 为什么不自己试试呢?您所说的“有效”是什么意思?什么对你很重要?怎么知道它是否有效?精度重要吗?
  • 问题是我有一半的代码在工作..这是一个大代码的一小部分..我试图避免数小时的调试

标签: c# winforms timer


【解决方案1】:

将计时器的间隔设置为 1000(即 1000 毫秒或 1 秒)。然后,当您启用它时,它会继续运行,每次经过时间间隔时都会触发 timer1_tick 事件。

下面是一个例子:

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;
    }
}

别忘了.启用计时器!

【讨论】:

  • @Dmitry:不,一旦启用计时器,它就会继续在表单上运行,直到您.Enabled = false它。
猜你喜欢
  • 2010-11-13
  • 2014-09-03
  • 2015-05-13
  • 2017-03-29
  • 2020-08-29
  • 2020-11-14
  • 2022-01-06
  • 1970-01-01
  • 2016-09-17
相关资源
最近更新 更多