如果你想重用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;
}
}
點擊哪個點擊? – Grzenio 2010-07-01 14:03:08
而你的問題的具體情況是......? – 2010-07-01 14:03:22
如果您想跟蹤給定按鈕上或您的應用或操作系統中的點擊,請添加。 – 2010-07-01 14:06:02