簡體   English   中英

C#-使用NumericUpDown作為間隔的倒數計時器

[英]C# - Countdown Timer Using NumericUpDown as Interval

我敢肯定這已經被問過了,但是我似乎找不到有效的解決方案。 我在表單上有一個NumericUpDown,還有一個標簽,一個計時器和一個按鈕。 我希望計時器在按下按鈕時啟動,並且計時器的間隔等於NumericUpDown和倒數的間隔將顯示在標簽中。 我知道這應該很容易。 有什么幫助嗎?

至今:

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

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

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

隨着計時器的計時,它似乎沒有更新。

這是您要尋找的快速示例。 這應該給您關於您需要做什么的基本想法

    //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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM