Question

I'm sure this has been asked before, but I cannot seem to find a solution that works. I have a NumericUpDown on my form and a label along with a timer and a button. I want the timer to start when the button is pressed and the interval for the timer to equal that of the NumericUpDown and a countdown will be displayed in the label. I know this should be easy. Any help?

So far:

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

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

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

It doesn't seem to update as the timer ticks.

Was it helpful?

Solution

Here is a quick example to what you are looking for. This should give you a basic idea on what you need to do

    //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();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top