2017-05-11 9 views
-2

私は窓の形に関する質問があります。私は下のコードと写真に見られるこのカウントダウンを作った。私はそれにも問題があります。スタートとストップで最初のカウントダウンを開始すると、時間、分、秒で実行するように実行されますが、2番目を実行するとすぐに最初のカウントダウンが2番目の値にリセットされます。そして、それは両方の秒をスキップします。したがって、最初の2番目の53から51に、もう1つの52から50に行く..いずれかの人がこの問題を解決し、それらを独立させる方法を知っていますか彼らは明らかに今お互いの?複数のタイマーがWindowsフォームで動作しないようですか?

ありがとうございます!

namespace newtime 
{ 
    public partial class Form1 : Form 
    { 
     private int h; 
     private int m; 
     private int s; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btnStart1_Click(object sender, EventArgs e) 
     { 

      if (textBox1.Text == "") 
      { 
       textBox1.Text = "0"; 
      } 
      if (textBox2.Text == "") 
      { 
       textBox2.Text = "0"; 
      } 
      if (textBox3.Text == "") 
      { 
       textBox3.Text = "0"; 
      } 

      h = Convert.ToInt32(textBox1.Text); 
      m = Convert.ToInt32(textBox2.Text); 
      s = Convert.ToInt32(textBox3.Text); 
      timer1.Start(); 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      s = s - 1; 
      if (s == -1) 
      { 
       m = m - 1; 
       s = 59; 
      } 
      if (m == -1) 
      { 
       h = h - 1; 
       m = 59; 
      } 
      if (h == 0 && m == 0 && s == 0) 
      { 
       timer1.Stop(); 
       MessageBox.Show("Times up!", "Timer"); 
      } 
      string hh = Convert.ToString(h); 
      string mm = Convert.ToString(m); 
      string ss = Convert.ToString(s); 
      textBox1.Text = hh; 
      textBox2.Text = mm; 
      textBox3.Text = ss; 
     } 

     private void btnStop1_Click(object sender, EventArgs e) 
     { 
      timer1.Stop(); 
     } 

     private void btnStart2_Click(object sender, EventArgs e) 
     { 

      if (textBox4.Text == "") 
      { 
       textBox4.Text = "0"; 
      } 
      if (textBox5.Text == "") 
      { 
       textBox5.Text = "0"; 
      } 
      if (textBox6.Text == "") 
      { 
       textBox6.Text = "0"; 
      } 
      h = Convert.ToInt32(textBox4.Text); 
      m = Convert.ToInt32(textBox5.Text); 
      s = Convert.ToInt32(textBox6.Text); 
      timer2.Start(); 
     } 

     private void timer2_Tick(object sender, EventArgs e) 
     { 
      s = s - 1; 
      if (s == -1) 
      { 
       m = m - 1; 
       s = 59; 
      } 

      if (m == -1) 
      { 
       h = h - 1; 
       m = 59; 
      } 

      if (h == 0 && m == 0 && s == 0) 
      { 
       timer2.Stop(); 
       MessageBox.Show("Times up!", "Timer"); 
      } 

      string hh = Convert.ToString(h); 
      string mm = Convert.ToString(m); 
      string ss = Convert.ToString(s); 
      textBox4.Text = hh; 
      textBox5.Text = mm; 
      textBox6.Text = ss; 
     } 

     private void btnStop2_Click(object sender, EventArgs e) 
     { 
      timer2.Stop(); 
     } 
    } 
} 

enter image description here

enter image description here

+6

あなたは2つのタイマーを持っていますが、あなたは常時上書きする 'h'、' m'、 's'変数の1セットしか持っていません。 – Adrian

+1

これはデバッガを使用し、その部分は少なくとも.. – MethodMan

+0

あなたはそのようなコードを繰り返すのを見るたびに、あなたはすぐに「私はおそらくクラスが必要です」と考えるべきです。 UserControlはここでうまくいくでしょう。 –

答えて

1

他のポスターが指摘したように、あなただけのすべてのタイマーイベントハンドラによって共有されているhm、およびsの一つのコピーを持っています。これは、現在見ているように、お互いの仕事を上書きすることを意味します。明白な修正は、それぞれのタイマーに対してh,m、およびsという異なるコピーを作成することです。

しかし、この問題を1つのタイマーで解決できることを指摘したいと思います。プログラムは、ユーザが各 "Start"ボタンをクリックしてそれを変数(1つのタイマー表示ごとに1つ)に格納するときに、時間を記録するだけです(System.DateTime.Nowから取得)。それでは、継続的に、現在の時刻とそれぞれの(格納された)開始時刻との間の差を計算するだけでよい。進行中の動作は、単一のタイマーとループによってトリガされる可能性があります。

アプリケーションでさまざまなタイマーを実行しないようにすることをお勧めします。それぞれのタイマーを個別に開始、停止、および廃棄することを心配する必要があるためです。

ここにはあなたにアイデアを伝えるための草案があります。コンパイルするには2つの調整が必要な場合があります。

struct TimerStatus 
{ 
    DateTime StartTime; 
    bool IsRunning; 
} 

TimerStatus[] _timers = new TimerStatus[10]; 


void Start1_Click() 
{ 
    _timers[1].StartTime = System.DateTime.Now; 
    _timers[1].IsRunning = true; 
} 

void Stop1_Click() 
{ 
    _timers[1].IsRunning = false; 
} 

void Start2_Click() 
{ 
    _timers[2].StartTime = System.DateTime.Now; 
    _timers[2].IsRunning = true; 
} 

void Stop2_Click() 
{ 
    _timers[2].IsRunning = false; 
} 

void OneAndOnlyTimer_Tick() 
{ 
    for (int i=0; i<=_timers.GetUpperBound(0); i++) 
    { 
     if (_timers[i].IsActive) 
     { 
      TimeSpan ts = System.DateTime.Now - _timers[i].StartTime; 
      DisplayTimer(i, ts.Hours, ts.Minutes, ts.Seconds); //You will need to write the method that does the display 
     } 
    } 
} 

余分な信用のためにあなたも(あなたがコントロールのTagプロパティを使用して配列のインデックスを識別することができます)すべてのボタンに同じクリックハンドラを使用することができます。しかし、それは私の答えの範囲外です。

関連する問題