2010-07-01 81 views
0

我有一個計時器,並在30分鐘內我要計數點擊並顯示在文本框中。但是如何?這裏是計時器代碼:c#點擊次數

decimal sure = 10; 
private void button1_Click(object sender, EventArgs e) 
{ 
    button1.Enabled = true; 
    timer1.Start(); 
} 

private void timer1_Tick(object sender, EventArgs e) 
{ 
    sure--; 
    label3.Text = sure.ToString(); 
    if (sure == 0) 
    { 
    timer1.Stop(); 
    MessageBox.Show("Süre doldu"); 
    } 
} 
+1

點擊哪個點擊? – Grzenio 2010-07-01 14:03:08

+0

而你的問題的具體情況是......? – 2010-07-01 14:03:22

+0

如果您想跟蹤給定按鈕上或您的應用或操作系統中的點擊,請添加。 – 2010-07-01 14:06:02

回答

0

在全局聲明您的clickCounter,並在鼠標單擊事件中提高您的計數器++。 如果你做得更具體,你可以使用Background Worker來追蹤時間。 並使用Application.DoEvents()將剩餘的寫入文本框 放置一個按鈕,2個標籤和一個計時器。與lblClickCount和lblRemainingTime

private int clickCounter = 0; 
    private void button1_Click(object sender, EventArgs e) 
    { 
     clickCounter++; 
     lblClickCount.Text = clickCounter.ToString(); 
    } 

    decimal sure = 10; 
    private void timer1_Tick(object sender, EventArgs e) 
    { 
     sure--; 
     lblRemainingTime.Text = sure.ToString(); 
     Application.DoEvents(); 
     if (sure == 0) 
     { 
      timer1.Stop(); 
      MessageBox.Show("Süre doldu. Toplam tiklama sayisi:" + clickCounter.ToString()); 
     } 
    } 
+0

Serkan bey kod olarakaçıklarmısınız? Genel olarak eksikolduğumkonu hertıklamadabirişlemyapmak。 mesela bir int a = 0; olsun。 1.tıklamadaa = 1,2.tıklamadaa = 2 olsun istiyorum。 – karatekid 2010-07-01 14:10:35

+0

我正在編輯我的帖子,編寫你想要的代碼 – 2010-07-01 14:15:42

+0

哪裏?我應該等一下嗎? thanx幫助.. – karatekid 2010-07-01 14:24:10

0

如果你想重用Button1以計算點擊次數,但不啓動新的計時器,如果周圍的代碼,你要保護你可以添加一個命名的標籤。

bool hasTimerStarted = false; 
int numberOfClicks = 0; 
private void button1_Click(object sender, EventArgs e) 
{ 
    if(!hasTimerStarted) 
    { 
     button1.Enabled = true; 
     timer1.Start(); 
     hasTimerStarted = true; 
    } 
    ++numberOfClicks; 
} 

當計時器到期時,您重置計數並且計時器已啓動。

private void timer1_Tick(object sender, EventArgs e) 
{ 

    TimeSpan ts = stopWatch.Elapsed; 

    // Format and display the TimeSpan value. 
    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", 
     ts.Hours, ts.Minutes, ts.Seconds, 
     ts.Milliseconds/10); 

    label3.Text = elapsedTime; 
    labelClicks.Text = "User clicked " + clicksNo.toString() + "nt times.."; 

    if (stopWatch.ElapsedMilliseconds >= this.minutes * 60 * 1000) 
    { 
     timer1.Stop(); 
     MessageBox.Show("Time elapsed."); 
     hasTimerStarted = false; 
     numberOfClicks = 0; 
    } 
}