2011-05-31 14 views
7

मैं 60 का एक पूर्णांक मान के साथ एक lblCountdown है मैं सेकंड के साथ lblCountDown कमी के पूर्णांक मूल्य बनाना चाहते जब तक यह तक पहुँचता है 0.सेकंड उलटी गिनती टाइमर

यह वही है मैं अब तक है:

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

    } 

उत्तर

13

उपयोग टाइमर इस

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 के लिए स्वतंत्र महसूस मदद की ज़रूरत है काम किया? – Kade

+0

आपको सार्वजनिक की जरूरत है? – Stecya

+0

मुझे यकीन नहीं है कि मुझे अपने कोड – Kade

0

के लिए कैसे .नेट फ्रेमवर्क से टाइमर वर्ग का उपयोग कर के बारे में? http://msdn.microsoft.com/en-us/library/0tcs6ww8.aspx

+0

के रूप में चिह्नित करने पर विचार करें हां एक सिस्टम टाइमर भी काम करता है, लेकिन उपरोक्त उदाहरणों जैसे विनफॉर्म संदर्भ में यह Winforms नियंत्रण को अपडेट करना सुरक्षित नहीं है। – Goodies

0

आपको प्रारंभ करने के लिए फॉर्म 1 के लिए एक सार्वजनिक कक्षा की आवश्यकता है।

इस कोड को देखें:

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 
     } 
    } 
} 

मैं इस की कोशिश की है और यह सब ठीक

आप अब और टिप्पणी करने के लिए :)

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"); 
} 
संबंधित मुद्दे