2011-05-31 15 views
7

tengo un lblCountdown con un valor entero de 60. Me quieren hacer que el valor int de la disminución lblCountDown con segundos hasta llegar a 0.segundos temporizador de cuenta atrás

Esto es lo que tengo hasta ahora:

private int counter = 60; 
    private void button1_Click(object sender, EventArgs e) 
    { 
     int counter = 60; 
     timer1 = new Timer(); 
     timer1.Tick += new EventHandler(timer1_Tick); 
     timer1.Interval = 1000; // 1 second 
     timer1.Start(); 
     label1.Text = counter.ToString(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     counter--; 
     if (counter == 0) 

      timer1.Stop(); 
      label1.Text = counter.ToString(); 

    } 

Respuesta

13

Uso del temporizador para este

private System.Windows.Forms.Timer timer1; 
    private int counter = 60; 
    private void btnStart_Click_1(object sender, EventArgs e) 
    { 
     timer1 = new System.Windows.Forms.Timer(); 
     timer1.Tick += new EventHandler(timer1_Tick); 
     timer1.Interval = 1000; // 1 second 
     timer1.Start(); 
     lblCountDown.Text = counter.ToString(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     counter--; 
     if (counter == 0) 
      timer1.Stop(); 
     lblCountDown.Text = counter.ToString(); 
    } 
+0

pivate Timer1 temporizador? – Kade

+0

usted necesita público? – Stecya

+0

no estoy seguro, ayúdame a verificar en mi código – Kade

0

Necesita una clase pública para que Form1 se inicialice.

ver este código:

namespace TimerApp 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private int counter = 60; 
     private void button1_Click(object sender, EventArgs e) 
     { 
      //Insert your code from before 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      //Again insert your code 
     } 
    } 
} 

He intentado esto y todo funcionaba bien

Si necesita más ayuda no dude en comentar :)

3
int segundo = 0; 
DateTime dt = new DateTime(); 

private void timer1_Tick(object sender, EventArgs e){ 
    segundo++; 
    label1.Text = dt.AddSeconds(segundo).ToString("HH:mm:ss"); 
} 
Cuestiones relacionadas