简体   繁体   English

当Timer Control中的时间结束时,什么事件对行动负责?

[英]What event is responsible for actions when time is out in Timer Control?

My program is some kind of test. 我的程序是某种测试。 The time of passing test is limited(20 minutes). 通过考试的时间有限(20分钟)。 When the time is out, the test must be finished and MessageBox appears with results of test. 当时间结束时,必须完成测试并显示带有测试结果的MessageBox In Form_Load : Form_Load

timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = (1000) * (1);
timer1.Enabled = true;
timer1.Start();

private void timer1_Tick(object sender, EventArgs e)
        {            
            timer_label.Text = Convert.ToString(time);
            --time;
        }

How to finish test when time == 0 ? 如何在time == 0时完成测试? And why time in timer_label changes with step 2?(eg 1999, 1997, 1995...) 为什么timer_label时间会随着步骤2的变化而变化?(例如1999年,1997年,1995年......)

How to finish test when time == 0? 如何在时间== 0时完成测试?

Timer just raises even on some interval. 定时器只是在某个时间间隔内提升。 You should start timer for that. 你应该为此启动计时器。 If you don't want events to be raised anymore, you should stop timer. 如果您不想再提出事件,则应该停止计时器。 You can do it directly in Tick event handler: 您可以直接在Tick事件处理程序中执行此操作:

private void timer1_Tick(object sender, EventArgs e)
{            
    timer_label.Text = Convert.ToString(time);
    time--;

    if (time == 0)
       timer1.Stop();
}

Second question: 第二个问题:

And why time in timer_label changes with step 2?(eg 1999, 1997, 1995...) 为什么timer_label中的时间会随着步骤2的变化而变化?(例如1999年,1997年,1995年......)

From your code there is no reason for such behavior. 从您的代码中没有理由这样的行为。 Looks like you have subscribed to two timers, or you have two event handlers of same timer Tick event. 看起来您订阅了两个计时器,或者您有两个相同计时器Tick事件的事件处理程序。 Also make sure you don't have decrement operator when displaying time, something like this: 还要确保在显示时间时没有递减运算符,如下所示:

timer_label.Text = Convert.ToString(--time);
--time;

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

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