0

我确定以前有人问过这个问题,但我似乎找不到有效的解决方案。我的表单上有一个 NumericUpDown 和一个标签以及一个计时器和一个按钮。我希望计时器在按下按钮时启动,并且计时器的间隔等于 NumericUpDown 的间隔,并且标签中将显示倒计时。我知道这应该很容易。有什么帮助吗?

至今:

   int tik = Convert.ToInt32(TimerInterval.Value);

    if (tik >= 0)
    {
        TimerCount.Text = (tik--).ToString();
    }

    else
    {
        TimerCount.Text = "Out of Time";
    }

随着计时器的滴答声,它似乎没有更新。

4

1 回答 1

2

这是您要查找的内容的快速示例。这应该让您对需要做什么有一个基本的了解

    //class variable
    private int totNumOfSec;

    //set the event for the tick
    //and the interval each second
    timer1.Tick += new EventHandler(timer1_Tick);
    timer1.Interval = 1000;


    private void button1_Click(object sender, EventArgs e)
    {
        totNumOfSec = (int)this.numericUpDown1.Value; 
        timer1.Start();
    }

    void timer1_Tick(object sender, EventArgs e)
    {
        //check the timer tick
        totNumOfSec--;
        if (totNumOfSec == 0)
        {
            //do capture
            MessageBox.Show("Captured");
            timer1.Stop();
        }
        else
            label1.Text = "Caputring in " + totNumOfSec.ToString();
    }
于 2009-10-18T16:11:07.907 回答