Reputation: 515
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++;
}
Upvotes: 2
Views: 1187
Reputation: 73
You could use the Tag property of the Button to set a flag for that logic you want to create. on the button click event
if (btnExample.Tag==0)
{
btnExample.Tag=1;
//call startCountDown function
}
else
{
btnExample.Tag=0;
// call reset
}
Upvotes: 1
Reputation: 16575
I would check if the timer is enabled
if (!timer2.Enabled) StartTimer2();
else ResetTimer2();
Upvotes: 1
Reputation: 352
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
Upvotes: 1