First button click to start timer, second to stop

2020-05-06 11:26发布

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

标签: c# timer
5条回答
虎瘦雄心在
2楼-- · 2020-05-06 12:02

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();
}
查看更多
霸刀☆藐视天下
3楼-- · 2020-05-06 12:04

you can use this !!!!

private void button8_click(object sender, EventArgs e)
{
   if (timer1.Enabled) {
       timer1.Stop();
   } else {
     timer1.Start();
   }
}
查看更多
Anthone
4楼-- · 2020-05-06 12:11

Use the Enabled property:

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

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

查看更多
甜甜的少女心
5楼-- · 2020-05-06 12:15

One line of code:

timer1.Enabled = !timer1.Enabled;
查看更多
一夜七次
6楼-- · 2020-05-06 12:17

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!!

查看更多
登录 后发表回答