[英]Multiple countdown timer + button
NOTE: This question is related to this one as well 注意:此问题也与此相关
I have a timer which is activated by a button that starts the countdown of the supposed activity. 我有一个计时器,该计时器由一个按钮激活,该按钮可启动假定活动的倒计时。 But I have a problem, when I press the same button again, the program must use another time (specified inside a datagrid) and start the countdown again, and if I press the button again, another time and so on.
但是我有一个问题,当我再次按下相同的按钮时,程序必须使用另一时间(在数据网格内指定)并再次开始倒计时,如果我再次按下该按钮,则需要进行其他操作,依此类推。
Shall I use multiple timers or is there a way I can use the same timer, but with new ("reset") values if I press the button? 我应该使用多个计时器,还是可以使用相同的计时器,但是如果我按下按钮,可以使用新的(“重置”)值吗?
(If you guys want me to show more of the code, just tell me I'll post here) (如果你们希望我显示更多代码,请告诉我我将在此处发布)
private bool timeSet2 = false;
int f = 1;
private void timer3_Tick(object sender, EventArgs e)
{
DateTime timeConvert;
DateTime dateTime = DateTime.Now;
string timeOp = dataGridView1.Rows[f].Cells[2].Value + "";
f++;
if (!timeSet2) // only get the value once
{
DateTime.TryParse(timeOp, out timeConvert);
milliSecondsLeft = (int)timeConvert.TimeOfDay.TotalMilliseconds;
timeSet2 = true;
}
milliSecondsLeft = milliSecondsLeft - 1000;
if (milliSecondsLeft > 0)
{
var span = new TimeSpan(0, 0, 0, 0, milliSecondsLeft);
lblLeft.Text = span.ToString(@"hh\:mm\:ss");
}
else
{
timer3.Stop();
}
I need to fit a button right here so if I press it, my program will start another countdown. 我需要在此处安装一个按钮,因此如果按下该按钮,程序将开始另一个倒计时。 But I don't know if I'll have to create another time for this.
但是我不知道是否需要为此再花时间。
You can use the same timer and reset it for each countdown. 您可以使用相同的计时器,并在每次倒计时时将其重置。 But I think you are misunderstanding the the functionality of the timer.
但是我认为您误解了计时器的功能。 The
timer_Tick
event occurs every time the interval of the timer has elapsed. 每当定时器的时间间隔过去时,就会发生
timer_Tick
事件。 Update the milliSecondsLeft
variable on your button click event. 在按钮单击事件上更新
milliSecondsLeft
变量。
You have to move some code to the button_Click
event. 您必须将一些代码移到
button_Click
事件。
private void button1_Click(object sender, EventArgs e)
{
milliSecondsLeft = Convert.ToInt32(dataGridView1.Rows[f].Cells[2].Value)*1000;
f++;
timer3.Start();
}
Your timer_Tick
event would then look like: 您的
timer_Tick
事件将如下所示:
private void timer3_Tick(object sender, EventArgs e)
{
milliSecondsLeft = milliSecondsLeft - 1000;
if (milliSecondsLeft > 0)
{
var span = new TimeSpan(0, 0, 0, 0, milliSecondsLeft);
lblLeft.Text = span.ToString(@"hh\:mm\:ss");
}
else
{
timer3.Stop();
}
}
Some other things: 其他一些事情:
dataGridView
with int f = 1;
dataGridView
int f = 1;
dataGridView
的第二列开始吗int f = 1;
dataGridView
to be in seconds. dataGridView
的倒计时时间以秒为单位。 But perhaps your code is right for your purpose
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.