PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Timer help please!!-VBForums
Results 1 to 18 of 18

Thread: Timer help please!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Timer help please!!

    Hey guys, i am making a simple. Math game in vb 2008 express addition for school. And i need help with the timers.
    You click start the sums on form4 and finish on form13. I want to have it show how long it took them to complete all the sums, i want it to show thatt time on form 14
    . I am new to vb so if someone could give me the code, it would be very nice!!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,417

    Re: Timer help please!!

    Welcome to VBForums

    Thread moved from the 'CodeBank' forum (which is for you to post working code examples, not questions) to the 'VB.Net' (VB 2002 and later) forum

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,741

    Re: Timer help please!!

    That's not what a Timer is for. A Timer is like an alarm clock, but what you're asking for is something like a stopwatch. Fortunately, there is a Stopwatch class. You create an instance, call Start, call Stop and get the Elapsed property. Elapsed is a TimeSpan, which you can call ToString on to get a String to display.

    You don't necessarily have to use a Stopwatch though. For simple scenarios, where you don't have to stop and start in between, you may prefer simply to store the current time (Date.Now) when you start and then subtract that from the current time when you finish. That will also produce a TimeSpan. That's basically what a Stopwatch does internally anyway.

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Re: Timer help please!!

    Thanks, i will try add this in today, i might need some more help soon

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Re: Timer help please!!

    Sorry for the double post, but could you maybe give me the code, as i have only been into vb for a few weeks for school. If you give me that code, it would be very nice of you, cheers.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,741

    Re: Timer help please!!

    I tend not to do people's home work for them. I've given you enough information to have a go so do that and, if it doesn't work then show us what you did and tell us what happened and we can help you fix it.

  7. #7
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,347

    Re: Timer help please!!

    Ill start you off:

    vb Code:
    1. Dim sw As New Stopwatch
    2. 'start stopwatch
    3. 'do stuff you want to time
    4. 'stop stopwatch
    5. 'get and display timespan of stopwatch

    jmcilhinney's advice should help you fill in the blanks

    Kris

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Re: Timer help please!!

    Err. Cheers, as i said ive only been doing vb for 2 weeks or so, ill try add it, can a button start the stop watch

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,918

    Re: Timer help please!!

    Yes. The stopwatch has a .Start method, which you would call in the button click event handler. It also has a .Stop method, and you can get the elapsed time in a variety of different formats. Personally, I like the millisecond format, and that may be pretty nearly what you want, too, except that you'd probably want to get seconds by dividing the milliseconds by 1000. After all, if the time taken is so long that it would make sense to look at anything other than seconds, either your math game is really tough, or the people using it really suck.

    EDIT: By the way, search around the forum on Stopwatch to find examples. They aren't all that common, since the object isn't used all that often, but it is also simple to use, so any example will probably be sufficient. Looking at MSDN for Stopwatch would also probably cover your needs.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Re: Timer help please!!

    Ohh wow, this is confusing, haha. Got msn? So we can talk easier

  11. #11
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Timer help please!!

    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  12. #12

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Re: Timer help please!!

    Thanks^^ itz abit confusing stil

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,918

    Re: Timer help please!!

    Toss something together and show us. We can provide guidance better that way.
    My usual boring signature: Nothing

  14. #14
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,335

    Re: Timer help please!!

    What's confusing? Think of a real life stop watch. It has a start event, Stop event and you can view the elapsed time by the display.

    Create the new stop watch
    Start the stop watch
    Stop the stop watch
    Display the elapsed time of the stop watch.

  15. #15

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Re: Timer help please!!

    K need help adding e display thing for elapsed

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,918

    Re: Timer help please!!

    You probably want elapsed milliseconds. That's almost certainly too much precision, but people might like that. However, you'd want to show it in seconds, so you would take the elapsed milliseconds and divide by 1000. That will result in a double. It would look something like this:
    Code:
    'After you stop the stopwatch:
    <whatever form should show the number>.<the label>.Text = (<your stopwatch>.elapsedmilliseconds/1000).ToString("N3")

    That's close. I forget what the actual property is called, but I think it has both elapsed and milliseconds in it, so that is probably close, if not right. The .ToString will convert that number into a string. The argument after that will give you a pretty pleasing format with three decimal places. You might use fewer decimal places if you prefer.
    My usual boring signature: Nothing

  17. #17

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Re: Timer help please!!

    K it worked, how do i get it to show, like 2:32 2 mins 32 secs

  18. #18
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,741

    Re: Timer help please!!

    Quote Originally Posted by Aaron101 View Post
    K it worked, how do i get it to show, like 2:32 2 mins 32 secs
    As I said back in post #3:
    Elapsed is a TimeSpan, which you can call ToString on to get a String to display.
    The documentation for the TimeSpan.ToString method explains how to use it, as the documentation for every method does.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width