2016-12-27 78 views
0

有基本的Windows窗體應用程序一點點代碼我寫的。但有些事情出錯了。我開始的應用程序,一切都很好,但如果我啓動計時器,它的第一次運行,不重複。我該怎麼辦?的Visual C#計時器不滴答

對不起,語法錯誤。

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.Timers; 
using System.Windows.Forms; 

namespace Zamanlayıcı 
{ 
public partial class Form1 : Form 
{ 

    public int saat; 
    public int dakika; 
    public int saniye; 
    public int zaman; 

    public Form1() 
    { 
     InitializeComponent(); 

    } 

    private void baslat_button_Click(object sender, EventArgs e) 
    { 
     timer1.Interval = 1000; 
     timer1.Start(); 
    } 

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

    private void demizle_button_Click(object sender, EventArgs e) 
    { 
     saat_box.Clear(); 
     dakika_box.Clear(); 
     saniye_box.Clear(); 
     zaman_text.Text = "00:00:00"; 

    } 

    private void ses_checkbox_CheckedChanged(object sender, EventArgs e) 
    { 

    } 

    private void timer1_Tick_1(object sender, EventArgs e) 
    { 
     int saat = int.Parse(saat_box.Text); 
     int dakika = int.Parse(dakika_box.Text); 
     int saniye = int.Parse(saniye_box.Text); 
     int zaman = saat * 60 * 60 + dakika * 60 + saniye; 

     int saat2 = zaman/60/60; 
     int dakika2 = zaman/60 % 60; 
     int saniye2 = zaman % 60; 

     if(zaman > 0) { 
     zaman--; 
     zaman_text.Text = saat2.ToString("00") + ":" + dakika2.ToString("00") + ":" + saniye2.ToString("00"); 
     }if(zaman == 0) 
     { 
      MessageBox.Show("Zaman doldu!", "Bitti", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
     } 
    } 
} 
} 
+0

檢查以確保當我按下啓動按鈕的設置文本Tick事件勢必timer1_Tick_1 – Trey

+0

,但不重複所以我確定它必然正確的事件 –

+0

什麼是文本在那點..你有沒有在tick事件中放置一個斷點? – Trey

回答

4

其實定時器運行和timer1_Tick_1被稱爲每一秒。問題是,你在timer1_Tick_1,它總是得到相同的值saat * 60 * 60 + dakika * 60 + saniye;有一個局部變量zaman,所以你總是看到初始值。刪除本地變量,而僅使用字段zaman;使其初步測算計時器啓動時:

public partial class Form1 : Form { 

     public int saat; 
     public int dakika; 
     public int saniye; 
     public int zaman; 

     public Form1() { 
      InitializeComponent(); 

     } 

     private void baslat_button_Click(object sender, EventArgs e) { 
      timer1.Interval = 1000; 
      int saat = int.Parse(textBox1.Text); 
      int dakika = int.Parse(textBox2.Text); 
      int saniye = int.Parse(textBox3.Text); 
      zaman = saat * 60 * 60 + dakika * 60 + saniye; 
      timer1.Start(); 
     } 

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

     private void demizle_button_Click(object sender, EventArgs e) { 
      textBox1.Clear(); 
      textBox2.Clear(); 
      textBox3.Clear(); 
      textBox4.Text = "00:00:00"; 

     } 

     private void ses_checkbox_CheckedChanged(object sender, EventArgs e) { 

     } 

     private void timer1_Tick_1(object sender, EventArgs e) { 


      int saat2 = zaman/60/60; 
      int dakika2 = zaman/60 % 60; 
      int saniye2 = zaman % 60; 

      if (zaman > 0) { 
       zaman--; 
       textBox4.Text = saat2.ToString("00") + ":" + dakika2.ToString("00") + ":" + saniye2.ToString("00"); 
      } 
      if (zaman == 0) { 
       MessageBox.Show("Zaman doldu!", "Bitti", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      } 
     } 
    } 
+0

非常感謝,有時我們可能會錯過重要的東西:) –

1

像亞歷克斯說,你聲明zaman蜱事件處理中,所以它蜱每次(每一秒),它只是重置價值。您應該在tick事件處理程序之外聲明您的變量,並在其中進行修改。

public partial class Form1 : Form 
{ 
    public int saat; 
    public int dakika; 
    public int saniye; 
    public int zaman; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void baslat_button_Click(object sender, EventArgs e) 
    { 
     timer1.Interval = 1000; 
     int saat = int.Parse(textBox1.Text); 
     int dakika = int.Parse(textBox2.Text); 
     int saniye = int.Parse(textBox3.Text); 
     zaman = saat * 3600 + dakika * 60 + saniye;  //You were multiplying by 60 2x - just multiply by 3600 

     timer1.Start();  
    } 

    private void timer1_Tick_1(object sender, EventArgs e) 
    { 
     int saat2 = zaman/3600; //You were dividing by 60 2x - just divide by 3600 
     int dakika2 = zaman/60 % 60; 
     int saniye2 = zaman % 60; 

     if (zaman > 0) 
     { 
      zaman--; 
      textBox4.Text = saat2.ToString("00") + ":" + dakika2.ToString("00") + ":" + saniye2.ToString("00"); 
     } 
     if (zaman == 0) { 
      MessageBox.Show("Zaman doldu!", "Bitti", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
     } 

} 
相關問題