我有一个计时器,在30分钟内我想计算点击次数并在文本框中显示。但怎么样?这是计时器代码:
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");
}
}
答案 0 :(得分:0)
在全局声明您的clickCounter,并在鼠标单击事件中提升您的计数器++。 如果您更具体,可以使用后台工作程序来跟踪时间。 并使用Application.DoEvents()将剩余的内容写入textBox 放一个按钮,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());
}
}
答案 1 :(得分:0)
如果您想重复使用buttoN1来计算点击次数而不是启动新计时器,则可以在要保护的代码周围添加if。
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;
}
}