CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: StopWatch

  1. #1
    Join Date
    Oct 2000
    Location
    Upstate NY
    Posts
    249

    StopWatch

    The Following code is given in a major manual as code for a Stopwatch.

    Private Sub Command1_Click( )
    StartTime = Now ' both date and time
    Timer1.Enabeled = True
    End Sub

    Private Sub Command2_Click( )
    Timer1.Enabeled = False
    End Sub

    Sub Timer1_Timer( )
    Label1.Caption = Format$(Now StartTime,"hh:mm:ss")
    End Sub

    This doesn't work for two reasons.
    1 StartTime = Now brings forth Both date and time
    2 A mathematical operation is being performed on a string

    I have programmed my way around the problem but would like to know if anyone has a simple way to accomplish the above task.

    73 RSH



  2. #2
    Join Date
    Jul 2001
    Location
    Trivandrum, Kerala, India
    Posts
    21

    Re: StopWatch

    Hai,

    With minor modifications, your code worked very well. The following is the code.


    ' module level variable.
    Dim starttime as date

    private Sub Command1_Click()
    starttime = Now ' both date and time
    ' there was spelling mistake in
    ' the following line.
    Timer1.Enabled = true

    ' this line is added to set the
    ' interval
    Timer1.Interval = 200
    End Sub

    private Sub Command2_Click()
    Timer1.Enabeled = false
    End Sub

    Sub Timer1_Timer()
    ' a - is added between starttime and now
    Label1.Caption = Format$(starttime - Now, "hh:mm:ss")
    End Sub




    I hope this will work.

    Best of luck.

    Kishore.


  3. #3
    Join Date
    Nov 2001
    Location
    Little Rock AR
    Posts
    255

    Re: StopWatch

    I go back after programmers and optimize code that others have written. I use timers to see if my methods improve the speed of the logic.
    private sub ()
    'this records the time the event is triggered.
    StartTime = Time
    ' Code in here that I have rewritten or time delay
    'This records the time the event is stopped
    EndTime = time
    msgbox datediff("s", EndTime, StartTime)
    end sub

    This will get you the # of seconds that has elapsed.


    Hope is the feeling that you get that the feeling that you have will not last very long.

  4. #4
    Join Date
    Nov 2001
    Location
    Pan Yu city, GuangDong, China
    Posts
    47

    Re: StopWatch

    I would like to comment on all three, i.e. the question itself, and the two answers by LittleJohny and Kishore.

    First for the question:
    A solution may be...

    Dim StartTime as Double
    private Sub Form_Load()
    'May reduce to increase accuracy, but after a limit,
    'it will have no impact. Usually below 25-50.
    Timer1.Interval=200
    End Sub

    private Sub Command1_Click()
    StartTime = Now ' both date and time
    Timer1.Enabeled = true
    End Sub

    private Sub Command2_Click()
    Timer1.Enabeled = false
    End Sub

    private Sub Timer1_Timer()
    'This should be avoided for faster operations.
    'Only at stopping should be displayed the value.
    'However your's is a project of stopwatch, so
    'we MUST display a running count.
    Label1.Caption = Format$(Now-StartTime,"hh:mm:ss")
    End Sub




    Please note that Now (date+time) is stored as double internally, and you need not convert it for asignments or mathematical operations. Date is stored in integer part and no. of seconds elapsed since 12:00 AM in decimal part. Date function returns the integer part of it and time returns decimal part.

    Now about the solution that LittleJohny suggested. Please check the output of your stopwatch if it is started at 11:30 PM and stopped at 12:30 AM. This is because not including date part in the code. Using NOW solves the problem. Your code works best if started and stoped within the same date.

    And about the solution that Kishor provided. You are substracting NOW from StartTime which will always be lower then it. Revers the order, make it (Now-StartTime). Then you code is fine.


    With Regards,
    Prashant

    PS: Kindly rate my posts, no matter you like it or not (those zeros are also welcome), as it will help me evaluate and update my self.
    With Regards,
    Prashant Sharma

  5. #5
    Join Date
    Oct 2000
    Location
    Upstate NY
    Posts
    249

    Re: StopWatch

    Thanks To All:
    All of that was very helpful. Dont know why I kept coming up with a Type Mismatch with original code but I assumed it was the Subtraction of strings. All of the three code samples work very well and LittleJonny's could be useful for timing. I checked it with a (For Next)loop. Thanks again & 73
    RSH


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

Featured