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? 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
each time I press my button it goes 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++;
}