Search code examples
c#timer

First button click to start timer, second to stop


I'm struggling with making button first click to start a timer, second click to stop the timer and etc.

Can anybody help me? :)

private void button7_Click(object sender, EventArgs e)
{
    timer1.Start();
}

Solution

  • Use the Enabled property:

    if (timer1.Enabled) {
      timer1.Stop();
    } else {
      timer1.Start();
    }
    

    The Enabled property tells you if the timer is running or not.