Pergunta

Tenho certeza de que isso já foi perguntado antes, mas não consigo encontrar uma solução que funcione. Eu tenho um número numérico no meu formulário e um rótulo junto com um timer e um botão. Quero que o timer inicie quando o botão for pressionado e o intervalo para o timer igual ao do numericupdown e uma contagem regressiva será exibida no rótulo. Eu sei que isso deve ser fácil. Qualquer ajuda?

Até aqui:

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

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

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

Não parece atualizar como o timer ticks.

Foi útil?

Solução

Aqui está um exemplo rápido do que você está procurando. Isso deve lhe dar uma ideia básica sobre o que você precisa fazer

    //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();
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top