C# - Timer di conto alla rovescia che utilizza NumericUpdown come intervallo
Domanda
Sono sicuro che questo è stato chiesto prima, ma non riesco a trovare una soluzione che funzioni. Ho un NumericUpdown sulla mia forma e un'etichetta insieme a un timer e un pulsante. Voglio che il timer inizi quando il pulsante viene premuto e l'intervallo affinché il timer sia uguale a quello di NumericUpdown e un conto alla rovescia verrà visualizzato nell'etichetta. So che questo dovrebbe essere facile. Qualsiasi aiuto?
Finora:
int tik = Convert.ToInt32(TimerInterval.Value);
if (tik >= 0)
{
TimerCount.Text = (tik--).ToString();
}
else
{
TimerCount.Text = "Out of Time";
}
Non sembra aggiornare quando il timer spunta.
Soluzione
Ecco un rapido esempio di ciò che stai cercando. Questo dovrebbe darti un'idea di base su ciò che devi fare
//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();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow