2012-09-01 31 views
0

我有一個使用VS設計器聲明的System.Windows.Forms.Timer的.NET窗體。計時器工作正常。關閉窗體後,即使重新創建Timer對象,計時器也不會觸發事件。我已經將表單配置爲永不使用它:計時器在表格關閉後不會觸發

void MainFormFormClosing(object sender, FormClosingEventArgs e) 
    { 
     // never close 
     e.Cancel = true; 

     // only hide 
     this.Visible = false; 

    } 

如何製作計時器火災事件?我究竟做錯了什麼?

+0

你想做什麼,你需要計時器觸發時,窗體關閉? –

+0

因爲我稍後使用'this.Visible = true'顯示錶單。我需要計時器在那段時間內開火。 –

回答

1

我剛試過這個。 添加窗體上的一個的WinForms Timer組件調試窗口上負載啓動定時器,並調試當前時間。工作對我來說很好...

public frmTimer() 
{ 
    InitializeComponent(); 
} 
private void Form1_Load(object sender, EventArgs e) 
{ 
    timer1.Start(); 
} 
private void timer1_Tick(object sender, EventArgs e) 
{ 
    Debug.WriteLine(DateTime.Now.ToLongTimeString()); 
} 
private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    e.Cancel = true; 
    this.Visible = false; 
} 
+0

我終於設法使用'System.Timers.Timer'工作。 –