[英]Reset button in a countdown timer
I have a button, which I press and it starts a countdown. 我有一个按钮,我按下它,它开始倒计时。 But, if I press the same button again, the timer must reset and do another countdown (with another time defined by my program, but now this is irrelevant).
但是,如果我再次按下相同的按钮,计时器必须重置并进行另一次倒计时(由我的程序定义另一个时间,但现在这是无关紧要的)。
Is there any way I can do this reset inside the same button_click? 有没有什么办法可以在同一个button_click中重置这个? Maybe checking if the button was clicked again so I can reset the timer values?
也许检查按钮是否再次被点击,这样我可以重置计时器值?
I have this timer tick 我有这个计时器滴答
private int milliSecondsLeft = 0;
private int t = 0;
private bool timeSet = false;
private void timer2_Tick(object sender, EventArgs e)
{
string timeOp = dataGridView1.Rows[t].Cells[5].Value + "";
t++;
DateTime timeConvert;
DateTime dateTime = DateTime.Now;
if (!timeSet)
{
DateTime.TryParse(timeOp, out timeConvert);
milliSecondsLeft = (int)timeConvert.TimeOfDay.TotalMilliseconds;
timeSet = true;
timeSetNxt = false;
}
milliSecondsLeft = milliSecondsLeft - 1000;
if (milliSecondsLeft > 0)
{
var span = new TimeSpan(0, 0, 0, 0, milliSecondsLeft);
lblLeft.Text = span.ToString(@"hh\:mm\:ss");
}
else
{
timer2.Stop();
}
}
and this button_click 这个button_click
each time I press my button it goes t++;
每次按下我的按钮,它都会变成
t++;
, then it reads another time value on my datagrid. ,然后它在我的数据网格上读取另一个时间值。 thats why it must reset
这就是为什么它必须重置
int t = 1;
private void btn2_Click(object sender, EventArgs e)
{
timer2.Start();
lblLeft.Text = dataGridView1.Rows[t].Cells[5].Value.ToString();
string value = dataGridView1.Rows[t].Cells[5].Value.ToString();
lblLeft.Text = value.ToString();
t++;
}
You could use the Tag property of the Button to set a flag for that logic you want to create. 您可以使用Button的Tag属性为要创建的逻辑设置标志。 on the button click event
在按钮单击事件上
if (btnExample.Tag==0)
{
btnExample.Tag=1;
//call startCountDown function
}
else
{
btnExample.Tag=0;
// call reset
}
Show your Timer Code. 显示您的定时器代码。 To get the Number of resets.
获得重置次数。 Use code below.
使用下面的代码。
int button_clicked = new int();
private void button1_Click(object sender, EventArgs e)
{
// How many times you have Reset
button_clicked++;
// Your Timer Code
}
Just start a new Timer with Every click. 只需点击一下即可启动新的计时器。 Also, dispose the last one.
另外,丢弃最后一个。 You can use
button_clicked
to know if a timer has been started and hence dispose if the button_clicked > 0
您可以使用
button_clicked
来了解计时器是否已启动,因此如果button_clicked > 0
I would check if the timer is enabled 我会检查计时器是否已启用
if (!timer2.Enabled) StartTimer2();
else ResetTimer2();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.