Results 1 to 7 of 7

Thread: Timer Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    131

    Timer Question

    I would like to know how to get my timer to begin counting the elapsed time. Sort of like a stop watch. I don't want it displaying the time of day, just starting from 0, and going to 20 minutes.

    We have an application for our business center machine, and we want a small window on the desktop that will count the elapsed time the software is allowing them to use the machine before log off.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Timer Question

    Create a TimeSpan containing 20 minutes.
    Create a Stopwatch and Start it.
    Create a Timer and Start it.
    In the timer's Tick event handler subtract the Stopwatch's Elapsed time from the 20 minute TimeSpan and that's the time you have left. You can display that value in a Label or whatever.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: Timer Question

    What do you mean by Stop Watch? I thought you could only get timers!

    ------
    KNXRB

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Timer Question

    By Stopwatch I mean an instance of the Stopwatch class. It's not a component like a Timer, so you create it in code. A Stopwatch doesn't actually do anything while it's Running. It simply remembers the current time when you Start it. Each time you get its Elapsed property it simply subtracts that start time from the current time to get the elapsed time. See this thread for a similar implementation designed for .NET 1.x, before the Stopwatch class was added to the Framework.

    By the way, whenever I say "create a Thing" you can assume that I mean "create an instance of the Thing class", whatever "Thing" might be. I always use the proper names for types and members, including character casing. I think it's worth spending a few seconds more to post clearly.
    Last edited by jmcilhinney; Sep 9th, 2007 at 09:05 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: Timer Question

    thanks!

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    131

    Re: Timer Question

    That helped, but I do not know how to implement the code. I can not figure out how to get the timer to start. Where do I put each section of code?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Timer Question

    Have you read the documentation for the Timer class?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width