user3653415
user3653415

Reputation: 121

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();
}

Upvotes: 0

Views: 18312

Answers (5)

Ricardo Romo
Ricardo Romo

Reputation: 1624

you can use this !!!!

private void button8_click(object sender, EventArgs e)
{
   if (timer1.Enabled) {
       timer1.Stop();
   } else {
     timer1.Start();
   }
}

Upvotes: -1

Icemanind
Icemanind

Reputation: 48736

One line of code:

timer1.Enabled = !timer1.Enabled;

Upvotes: 6

SoftwareFactor
SoftwareFactor

Reputation: 8598

When you start a timer, its Enabled property is changed to True. And when you Stop it, it is set back to False. So you can use that to check the status of the timer.

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

Upvotes: 0

LarsTech
LarsTech

Reputation: 81675

Use the Enabled property:

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

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

Upvotes: 4

ke4ktz
ke4ktz

Reputation: 1312

Assuming you're using the System.Timers.Timer class, simply use:

private void button8_click(object sender, EventArgs e)
{
    timer1.Stop();
}

See the MSDN page for more handy methods!!

Upvotes: 0

Related Questions