dcsimg
Results 1 to 11 of 11

Thread: Stopwatch on multiple forms

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Stopwatch on multiple forms

    For starters, im not very good at vb..
    I am making a mThs game, that has 10 questions, all of different forms. Theres a button on the first forum saying, 'subtraction' addidtion and that. I need. Stop watch to start when you click one of those buttons , and stop once all questions have been answered, and the time shown.

    If someone could help me with this, maybe giving me some codd to start off from, would be nice.
    Add me on msn if u could help or post here : scape-x@hotmail.com

    Thanks

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

    Re: Stopwatch on multiple forms

    To use a stopwatch, there happens to be a Stopwatch class with a code example. If you need help with it we can help, but that link may be all you need.
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Re: Stopwatch on multiple forms

    Err, looks hard, aha. How long dows it take to add?

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

    Re: Stopwatch on multiple forms

    It's trivial. If you can add variables, you can add a stopwatch. Over in the .NET CodeBank, I have a Profiler class (you can search on my name and profiler to find it) that uses a stopwatch. Of course, there's a bunch of other things in there, but that class would also show you how to instantiate a stopwatch (one line using New), and would show you lines to Stop and Start.

    The stopwatch class is pretty much the exact same as a physical stopwatch. You create one, then you start it whenever you think the timing should begin, and you stop it whenever you think the timing should stop. You can also pause it. Finally, you read the amount of time elapsed. That's where it can be almost tricky, because the output can be in so many different formats. Fortunately, you only really want the elapsed milliseconds.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Re: Stopwatch on multiple forms

    Ok, you got msn?

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

    Re: Stopwatch on multiple forms

    I doubt any one will add you on MSN for home work help. As SH has said. Try your self and post up when you get stuck. There is no way you could of read the stop watch class by now. It contains all you need.

    Code:
            Dim stopWatch As New Stopwatch()
            stopWatch.Start()
            Thread.Sleep(10000)
            stopWatch.Stop()

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Re: Stopwatch on multiple forms

    I couldnt find his class thing, and that code you said, where do i add that?

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

    Re: Stopwatch on multiple forms

    The stop watch class is posted above in second post. lol

    http://msdn.microsoft.com/en-us/libr...stopwatch.aspx

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Re: Stopwatch on multiple forms

    Ohh, i read most of that.. I just still dont get how to have the stopwatch start on form4 and stop on form13

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

    Re: Stopwatch on multiple forms

    Oh, that IS a bit more tricky. For that to work, the stopwatch must be visible to all the forms. Putting it on any one form won't do. The best thing to do would be to add a module to the project (Project|Add Module). You can name it whatever you want. A module is a code page where every public item is visible to all objects in the project. Therefore, if you declare the stopwatch as Public in a module, it can be seen by all the forms. Any form can stop it and start it. This is known as a global variable. Global variables are discouraged, as a general rule, because they can lead to confusion and problems in larger projects. However, they are ideal for your particular situation, so go right ahead and use one.

    Once you have added the module, and added the stopwatch to the module:

    Public someStopwatchName As New Stopwatch

    then you are ready to start working with the stopwatch. Call the start method in the form where you want it to start. Call the stop method in the form where you want it to stop, then call one of the Elapsed methods wherever you want to display the elapsed time.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Re: Stopwatch on multiple forms

    Ive successfully added the module, started the stopwatch and stopped it, now how do i get it to show the elapsed? Do i use a label or somthing?
    Last edited by Aaron101; Oct 16th, 2011 at 05:33 PM.

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