2016-12-05 2 views
-1

私はC#プロジェクトに取り組んでいます。 MainForm.csでは私はタイマーを使います。 60秒後、タイマー制御情報と新しいフォームが表示されます。新しいフォームを表示した後にタイマーが機能しなくなるのはなぜですか?

 private void timer1_Tick(object sender, EventArgs e) 
    { 
     timer1.Enabled = true; 
     timer1.Interval = 60000; 

     ///new message if exist 

     ...Check Database Statement 
      frm_newmessage frm_message = new frm_newmessage(); 
      frm_show.ShowDialog(); 
     } 

    } 

新しいメッセージフォームを表示してメインフォームタイマに戻った後、動作が停止します。私は戻ってMainFormに近い新しいメッセージフォームの後に再びタイマスタートとするにはどうすればよい

private void MainForm_Load(object sender, EventArgs e) 
    { 
     timer1_Tick(sender, e); 
    } 

おかげで、高度な

+1

@DreamWorks、「あなたは良いブランドタイマーを買うべきですか?あなたのコメントはあなたのタイマーのコメントを「リセットする必要があります」を過ぎても全くゼロになってしまいます。 – MethodMan

+0

@MethodManしかし、それはあなたの笑いを思い起こさせ、笑いは最高の解決策です – DreamWorks

+0

タイマーを使う方法について@また、 'start、stop、enabled、disabled'プロパティにも注意してください。 – MethodMan

答えて

1

にあなたは、メインフォームのロードであなたのタイマーを起動する必要があります。 Tickイベントハンドラは、Xミリ秒ごとに1回呼び出されます(XはタイマーのIntervalプロパティを設定したものです)。 Tickイベントが発生すると、タイマーを停止し、表示されたダイアログウィンドウが閉じられた後に、タイマーを再び開始する必要があります。

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

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

      timer1.Interval = 2000; 
      timer1.Enabled = true; 
      timer1.Start(); 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      timer1.Stop(); 
      frm_newmessage frm_message = new frm_newmessage(); 
      frm_message.ShowDialog(); 
      timer1.Start(); 
     } 
    } 
} 
+1

ありがとうございます。それはうまく動作します – saeed

関連する問題