2

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++;
}
MattDAVM
  • 505
  • 3
  • 6
  • 25
  • Where is your code! What have you tried so far? – Roman Marusyk May 04 '16 at 11:11
  • To reset `timer2` simply call `Stop()` and then `Start()` (if you need to run "get value once" logic, then reset `timeSet`). The logic and problem is not very clearly explained (e.g. what is `timer3` suddenly?). – Sinatr May 04 '16 at 11:39
  • @Sinatr just changed the number in a few tests, forgot to change it back to the original value, sorry D: – MattDAVM May 04 '16 at 11:50
  • Possible duplicate of [How to reset a timer in C#?](http://stackoverflow.com/questions/1042312/how-to-reset-a-timer-in-c) – Sinatr May 04 '16 at 11:54
  • @Sinatr I tried to use Dan's answers in that question but it didn't changed my time value. When I press the button more than once, my timer stills on the first value that it started. it doesn't reset and goes to another time value. – MattDAVM May 04 '16 at 11:57
  • This is because you don't reset logic inside timer tick event handler (I guess you have to set `timeSet = false` for this). Note you always use `Row[0]` in timer. – Sinatr May 04 '16 at 12:00
  • @Sinatr sorry, it was another part of the code written down. It was a variable called "t" instead of 0, so each time I press my button it goes `t++;`, then it reads another time value on my datagrid. thats why it must reset – MattDAVM May 04 '16 at 12:34

3 Answers3

1

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
}
Bogoljub
  • 73
  • 6
1

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

Abhishek Kumar
  • 342
  • 5
  • 22
  • @MattDAVM It would be appreciated if you ask the question more carefully. You say `"If I press the button again, now I want this value to become like "11:13:46"` but why you want that. What is the Logic. – Abhishek Kumar May 04 '16 at 11:26
  • I have a datagrid which has some time values, each time I press the button, this value is going to be used as a timer countdown. Since I have more than a single value on it, each time I press the button, the program must reset the timer and read the next line and transform this value into a timer countdown. – MattDAVM May 04 '16 at 11:55
1

I would check if the timer is enabled

if (!timer2.Enabled) StartTimer2();
else ResetTimer2();
Mike Miller
  • 16,195
  • 1
  • 20
  • 27