First button click to start timer, second to stop [closed]

早过忘川 提交于 2019-12-20 07:20:35

问题


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

回答1:


Use the Enabled property:

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

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




回答2:


One line of code:

timer1.Enabled = !timer1.Enabled;



回答3:


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




回答4:


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



回答5:


you can use this !!!!

int x = 0;
private void button8_click(object sender, EventArgs e)
{
   if (x == 0)
    {
    timer1.Start();
    x = 1;
    }

  else if(x == 1)
    {
    timer1.Stop();
    x = 0;
    }
}


来源:https://stackoverflow.com/questions/23744842/first-button-click-to-start-timer-second-to-stop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!