2011-09-13 162 views
2

我希望我輸入的不同時間間隔有不同的定時器。例如,如果我輸入4個,4個定時器在4個標籤中創建並顯示時間,其中第1個定時器的時間在1秒內變化,第2個在2秒延時自拍時間的變化,在3秒3定時器的時間變化和4sec.Here 4tn計時器的時間變化是我的代碼,C#中不同時間間隔的多個定時器.net

 string input = textBox2.Text; 
     int n = Convert.ToInt32(input); 
     for (i = 1; i <= n; i++) 
     { 

      Timer timer = new Timer(); 
      timer.Tick += new EventHandler(timer_Tick); 
      timer.Interval = (1000) * (i);    


      timer.Enabled = true;      
      timer.Start();        



      Label label = new Label();    
      label.Name = "label"+i; 
      label.Location = new Point(100, 100 + i * 30); 
      label.TabIndex = i; 
      label.Visible = true; 


      this.Controls.Add(label); 

     } 
    private void timer_Tick(object sender, EventArgs e) 
     { 

      label.Text = DateTime.Now.ToString(); 


     } 

但我沒有得到任何output.What可我怎麼do.I使用Windows應用程序。

+0

請正確指定*正在使用哪個Timer類(完全限定)。有很多人的表現略有不同。 – 2011-09-13 04:19:25

+0

我不明白這是如何編譯的,它不會給'timer_Tick'中的'label'使用錯誤嗎? –

+0

@Hernal Pandya陰影的樂趣;-) – 2011-09-13 04:20:25

回答

0

另一種方法來檢索,比向你顯示的Bala R保持定時器和標籤對的字典(dict)中的變量的引用,並且通過發件人引用訪問事件處理程序(完整的源代碼,帶有2個文本框和一個按鈕,第二個文本框包含文本「 3「),希望它會有所幫助:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace ProjectDocumentationWorkspace.UI 
{ 
    public partial class MainForm : Form 
    { 
     private Dictionary<Timer, Label> dict = new Dictionary<Timer, Label>(); 

     public MainForm() 
     { 
      InitializeComponent(); 
     } 

     private void CreateTimers() 
     { 
      string input = textBox2.Text; 
      int n = Convert.ToInt32(input); 
      for (int i = 1; i <= n; i++) 
      { 

       Timer timer = new Timer(); 
       timer.Tick += new EventHandler(timer_Tick); 
       timer.Interval = (1000) * (i); 

       Label label = new Label(); 
       label.Name = "label" + i; 
       label.Location = new Point(100, 100 + i * 30); 
       label.TabIndex = i; 
       label.Visible = true; 

       this.Controls.Add(label); 

       dict[timer] = label; 

       timer.Enabled = true; 
       timer.Start(); 
      } 
     } 

     private void timer_Tick(object sender, EventArgs e) 
     { 
      Timer t = (Timer)sender; 
      DateTime current = DateTime.Now; 
      dict[t].Text = string.Format("{0:00}:{1:00}:{2:00}", current.Hour, current.Minute, current.Second); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      CreateTimers(); 
     } 
    } 
} 
+0

It works.Thanks for help。 – sumona

+0

如果我想用datetime包含一些文本,字符串格式是什麼? – sumona

+0

如何使用Dictionary @artur?你能幫我嗎? – sumona

2

我沒有看到Timer的Tick事件處理程序如何訪問不在其範圍內的動態標籤。

嘗試

 Label label = new Label();    
     label.Name = "label"+i; 
     label.Location = new Point(100, 100 + i * 30); 
     label.TabIndex = i; 
     label.Visible = true; 

     Timer timer = new Timer(); 
     timer.Tick += (s, e) => label.Text = DateTime.Now.ToString(); 
     timer.Interval = (1000) * (i);    


     timer.Enabled = true;      
     timer.Start(); 

另一種方法是有一個Dictionary<Timer, Label>並且當它們被創建控件添加到這個字典和定時器的節拍處理程序使用字典中的相應Label

+0

您可能需要在UI線程上調用文本更改才能正常工作 –

+0

+1,通過C#(Label實例)中的引用捕獲本地變量實際上可以工作。 –

+0

@Valentin不是System.Forms.Timer的總是在UI線程上打勾? –