6 Replies - 3589 Views - Last Post: 22 October 2009 - 08:20 AM

#1 ho-ho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 69
  • Joined: 04-May 09

Need help restarting a timer - C#

Post icon  Posted 22 October 2009 - 06:35 AM

I have a timer doing countdown from 100. I am writing a restart method which one called, should restart the countdown. How can I do that. Thanks for any help.


private int countDown = 100;
private string timerString = "Time Left: ";

private void timer1_Tick(object sender, EventArgs e)
		{

countDown = countDown - 1;
			timerString = "Time Left: " + countDown;

}

private void restart()
		{
		 
  // need to restart the timer, once this method is called. Please help.

}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help restarting a timer - C#

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Need help restarting a timer - C#

Posted 22 October 2009 - 07:40 AM

View Postho-ho, on 22 Oct, 2009 - 07:35 AM, said:

I have a timer doing countdown from 100. I am writing a restart method which one called, should restart the countdown. How can I do that. Thanks for any help.


private int countDown = 100;
private string timerString = "Time Left: ";

private void timer1_Tick(object sender, EventArgs e)
		{

countDown = countDown - 1;
			timerString = "Time Left: " + countDown;

}

private void restart()
		{
		 
  // need to restart the timer, once this method is called. Please help.

}




If you just want to restart the timer control, meaning you have stopped the timer and want to start it again, just call timer1.Start().

private void restart()
{
	timer1.Start();
}



Also, if you want to restart the countDown back to its original value as well just add that code in.

private void restart()
{
	countDown = 100;
	timer1.Start();
}


Was This Post Helpful? 0
  • +
  • -

#3 ho-ho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 69
  • Joined: 04-May 09

Re: Need help restarting a timer - C#

Posted 22 October 2009 - 07:50 AM

when I type timer1.Start() in the restart method , it gives an error saying
"System.Windows.Forms.Timer does not have a definition for Start"
Thanks for helping.
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Need help restarting a timer - C#

Posted 22 October 2009 - 07:53 AM

View Postho-ho, on 22 Oct, 2009 - 08:50 AM, said:

when I type timer1.Start() in the restart method , it gives an error saying
"System.Windows.Forms.Timer does not have a definition for Start"
Thanks for helping.


Forgot this was mobile. Try using:

timer1.Enabled = true;


Was This Post Helpful? 0
  • +
  • -

#5 ho-ho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 69
  • Joined: 04-May 09

Re: Need help restarting a timer - C#

Posted 22 October 2009 - 08:06 AM

whn the app. starts it's doing countdown perfectly but I tried this code and timer won't restart in the restart().


 private void timer1_Tick(object sender, EventArgs e)
		{
		 countDown = countDown - 1;
			timerString = "Time Left: " + countDown;
}

private void restart()
		{

timer1.Enabled = true;
			countDown = countDown - 1;
			timerString = "Time Left: " + countDown;

}


Was This Post Helpful? 0
  • +
  • -

#6 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Need help restarting a timer - C#

Posted 22 October 2009 - 08:13 AM

The code for your Restart method is not quite right. When you restart the timer you want to set the countDown variable back to its original value I believe.

private void restart()
		{

timer1.Enabled = true;
			countDown = 100;

}



Could you post the code where you stop the timer when it reaches 0?
Was This Post Helpful? 0
  • +
  • -

#7 ho-ho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 69
  • Joined: 04-May 09

Re: Need help restarting a timer - C#

Posted 22 October 2009 - 08:20 AM

Thanks for the help. I wasn't changing the original value of the variable in the restart(). It works now.
Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1