Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

timer control

timer countdown C#

  • Please log in to reply
6 replies to this topic

#1 sbuphonik

sbuphonik

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:C#, Pascal, Others
  • Learning:Java, C#, Delphi/Object Pascal, Pascal, Others

Posted 16 August 2012 - 07:58 AM

Hello world I am a novice in c# and would like help on coding for a 30 second countdown timer.
Thank you.
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 16 August 2012 - 08:05 AM

What do you have so far and what specifically do you need help with? Have you checked out the System.Timers.Timer class?
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 16 August 2012 - 08:05 AM

I think you can do Timer timer = new Timer();

then set your timer to timer.Interval = 1000; //

then add the Tick event, each Tick is 1 second ... have a counter that would count up to 30.

I'm not sure if you make timer.Interval = 30000. ... then you only need to have 1 Tick.

Give it a try, hope this helps
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#4 sbuphonik

sbuphonik

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:C#, Pascal, Others
  • Learning:Java, C#, Delphi/Object Pascal, Pascal, Others

Posted 16 August 2012 - 08:09 AM

I have a timer control,label for output and button to start the countdown since I am a student and new at this I have no clue on how to add code for counting down and for it to be displayed as output on the label
  • 0

#5 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 16 August 2012 - 08:17 AM

Check out the documentation page for the Timer control:
http://msdn.microsof...r(v=vs.71).aspx
You'll find examples for how to use it. You'll need to write a method which will get executed every time the Timer's interval expires. You could use this method to update the display, check variables, whatever you want. Then, tell the timer the name of the method you want it to execute by creating a new ElapsedEventHandler as in this example taken from the doc page:
public class Timer1
 {

     public static void Main()
     {
         System.Timers.Timer aTimer = new System.Timers.Timer();
         aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
         // Set the Interval to 5 seconds.
         aTimer.Interval=5000;
         aTimer.Enabled=true;

         Console.WriteLine("Press \'q\' to quit the sample.");
         while(Console.Read()!='q');
     }

     // Specify what you want to happen when the Elapsed event is raised.
     private static void OnTimedEvent(object source, ElapsedEventArgs e)
     {
         Console.WriteLine("Hello World!");
     }
 }

Note: As you are a beginner, here's the biggest tip that I think will help you: When you don't know how to do something, check the documentation pages for the classes you're using. You'll often find examples there, but even if you don't, just seeing the list of class methods and related classes often gives you vital clues as to how you're expected to use those classes. Always consult the documentation frequently.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#6 sbuphonik

sbuphonik

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:C#, Pascal, Others
  • Learning:Java, C#, Delphi/Object Pascal, Pascal, Others

Posted 16 August 2012 - 08:50 AM

@greg thank you for your help it helped a lot and will surely take your advice!!!!:)
  • 0

#7 CodeTiger

CodeTiger

    CC Regular

  • Member
  • PipPipPip
  • 25 posts
  • Programming Language:Java, C++, C#, JavaScript, Lua
  • Learning:Objective-C

Posted 28 August 2012 - 03:03 AM

If you work with WPF you can use this: http://msdn.microsof...dispatchertimer . I think it is more facile to use
  • 0





Also tagged with one or more of these keywords: timer, countdown, C#

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download