Results 1 to 28 of 28

Thread: [RESOLVED] Countdown Timer with minutes seconds and milliseconds

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    43

    Resolved [RESOLVED] Countdown Timer with minutes seconds and milliseconds

    hi everybody!
    i have to make a basketball scoreboard application in vb 6.
    i pretty much know how to do everything except for the countdown timer which has to count down all the way from 8 minutes to 0. it would also be nice if it makes a beep when it reaches 0. so here's the problem. how to make a timer with minutes seconds n milliseconds?? it should look like this
    minutes:seconds:milliseconds
    someone please help!! im desperate
    thanks in advance

  2. #2
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    259

    Re: Countdown Timer with minutes seconds and milliseconds

    you could put a timer control on the form, and set its interval to 1000.

    then do what's next
    Last edited by Kaimonington; Jan 5th, 2008 at 10:50 AM.

  3. #3
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    259

    Re: Countdown Timer with minutes seconds and milliseconds

    make a variable called time as single, and set it to 8.
    double click the timer, and type in this
    Code:
    time = time - 1
    if time = 0 then MsgBox "done", vbOKOnly + vbInformation, "done"
    text1.text = str(time)

  4. #4
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    259

    Re: Countdown Timer with minutes seconds and milliseconds

    I dont know about the beep, miliseconds are a bit dodgey because I tried that on my computer and it takes slightly longer than a milisecond because the timer has to do stuff aswell.

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Countdown Timer with minutes seconds and milliseconds

    Add Label control,Command button and Timer to your form first, then copy/paste and run this sample code:
    Code:
    Option Explicit
    
    Dim lSeconds As Long
    
    Private Sub Form_Load()
        lSeconds = 8 * 60000
        Label1.Caption = "00:80:00.000"
        Timer1.Interval = 1 '1/1000 of a sec
        Timer1.Enabled = False
    End Sub
    
    Private Sub Command1_Click()
        Timer1.Enabled = Not Timer1.Enabled
    End Sub
    
    Private Sub Timer1_Timer()
    Dim timeleft As String
    
        lSeconds = lSeconds - 1
        
        timeleft = Format((lSeconds \ 3600000), "00") & ":" & _
                  Format((lSeconds Mod 3600000) \ 60000, "00") & ":" & _
                  Format((lSeconds Mod 60000) / 1000, "##.000")
        
        Label1.Caption = timeleft
        If lSeconds = 0 Then Timer1.Enabled = False
    
    End Sub

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    43

    Re: Countdown Timer with minutes seconds and milliseconds

    thank you so much for that code!
    i have one tiny question left.
    when i try it...
    it displays
    00:08:00:000
    and the shows the seconds in the millisecond part... i only need 2 digits for millisecond and the seconds should show in the actual second section. i tried to play around with the numbers but i got 3 digits in the seconds instead.
    can you please help?
    i attached the files so you can see what actually happens...
    thanks a lot!!
    Attached Files Attached Files

  7. #7
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    259

    Re: Countdown Timer with minutes seconds and milliseconds

    ok, this code is alot longer than mine and has miliseconds, but when I do this, It doesnt do it every millisecond, it does it slightly more than a milisecond, I think because it needs time to change the textbox's text, but the seconds are more or less the same.

  8. #8

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Countdown Timer with minutes seconds and milliseconds

    Quote Originally Posted by Kaimonington
    ok, this code is alot longer than mine and has miliseconds, but when I do this, It doesnt do it every millisecond, it does it slightly more than a milisecond, I think because it needs time to change the textbox's text, but the seconds are more or less the same.
    What are you talking about?

  10. #10
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Countdown Timer with minutes seconds and milliseconds

    Final:

    Code:
    Option Explicit
    
    Dim lSeconds As Long
    
    Private Sub Form_Load()
        lSeconds = 8 * 60000
        Label1.Caption = "00:08:00.00"
        Timer1.Enabled = False
        Timer1.Interval = 1 '1/1000 of a sec
        Me.Caption = "StopWatch!"
    End Sub
    
    Private Sub Command1_Click()
        Timer1.Enabled = Not Timer1.Enabled
            If Timer1.Enabled = True Then Command1.Caption = "Stop"
            If Timer1.Enabled = False Then Command1.Caption = "Start"
    End Sub
    
    Private Sub Timer1_Timer()
    Dim timeleft As String
    
        lSeconds = lSeconds - 1
        
        timeleft = Format((lSeconds \ 3600000), "00") & ":" & _
                  Format((lSeconds Mod 3600000) \ 60000, "00") & ":" & _
                  Format((lSeconds Mod 60000) / 1000, "##.00")
        
        Label1.Caption = timeleft
        If lSeconds = 0 Then Timer1.Enabled = False
    
    End Sub

  11. #11

  12. #12
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: Countdown Timer with minutes seconds and milliseconds

    If you only need 2 places for the fraction of a second part then I recommend setting the timer interval to 10. That way that timer has more time to run the code and keep a more accurate time.

  13. #13
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Countdown Timer with minutes seconds and milliseconds

    A timer can't really run any faster than ~15 milliseconds. Set the interval to anything less and it runs at ~15 milliseconds. It's also not very accurate, the following is the results (in milliseconds) of timing a timer of interval 1 with QueryPerformanceCounter.

    15.65
    21.4
    9.77
    15.66
    17.67
    15.6
    15.63
    14.64
    23.39
    9.82

    and interval 15...

    15.08
    3.91
    18.6
    15.63
    15.55
    15.77
    13.61
    23.47
    12.66
    9.85

  14. #14
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Countdown Timer with minutes seconds and milliseconds

    Here's my effort, might need tweaking but should run much more accurately
    Code:
    Option Explicit
    
    Dim EndTime As Single
    
    Private Sub Command1_Click()
      EndTime = (Timer + 8 * 60) Mod 86400
      Timer1.Interval = 20
    End Sub
    
    Private Sub Timer1_Timer()
    Dim CtDn As Long
      CtDn = (EndTime - Timer) * 100
      If CtDn < 0 Then
        If CtDn < -1000 Then 'bit of faff to avoid over midnight countdown
          CtDn = CtDn + 8640000
        Else
          Label1.Caption = "0:00:00"
          Beep 'or somesuch
          Timer1.Interval = 0
          Exit Sub
        End If
      End If
      Label1.Caption = Format((CtDn \ 6000) Mod 60, "0:") & Format((CtDn \ 100) Mod 60, "00:") & Format(CtDn Mod 100, "00")
    End Sub
    I'm assuming hundredths of seconds in preference to milliseconds

  15. #15
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Countdown Timer with minutes seconds and milliseconds

    Milk is correct. The system Time is updated every 1/60 second (~ 16.67 milliseconds). So don't base on counter to display realtime.

    Code:
    Option Explicit
    
    Dim Finish As Single
    
    Private Sub Command1_Click()
       Finish = Timer + 8 * 60 '-- 8 minutes from now (in seconds)
       Label1.Caption = "8:00.00"
       Timer1.Interval = 10
       Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
       Dim CountDown As Single
       Dim s As Long
       
       CountDown = Finish - Timer '-- in seconds (with decimal part)
       If CountDown <= 0 Then
          Label1.Caption = "0:00.00"
          Timer1.Enabled = False
       Else
          s = Int(CountDown)
          Label1.Caption = (s \ 60) & ":" & Format(s Mod 60, "00") & _
                           Format(CountDown - s, ".00")
       End If
       DoEvents
    End Sub
    PS.: Need to be considered if Timer pass midnight.
    Last edited by anhn; Jan 6th, 2008 at 05:05 AM.

  16. #16
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Countdown Timer with minutes seconds and milliseconds

    anhn - Format(Counddown - s, ".00")

    Should be Format(Countdown - s, ".00") (typo )

  17. #17
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Countdown Timer with minutes seconds and milliseconds

    Have a look at my code for a crossing midnight tweak...

  18. #18

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    43

    Re: Countdown Timer with minutes seconds and milliseconds

    hey thanks guys!
    you really helped a lot. i took some advice from all of you and mixed up a little and got a nice working timer!
    thanks!

  19. #19
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds

    @ Milk, when posting Post#15, I didn't see your post (even after 33 minutes).
    They look very similar.

  20. #20

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    43

    Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds

    hi
    i already asked this to milk but i am getting no reply...
    with his code i can stop the timer, but when i click on the command again it restart from 8 minutes. do you know how to make it continue from where it stopped?
    thanks i really need this...

  21. #21
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds

    Make 2 commandbuttons (on top of each other, same size)

    Code:
    Option Explicit
    
    Dim EndTime As Single
    Dim Index As Integer
    
    Private Sub Command1_Click()
    Timer1.Enabled = True
    Command1.Visible = False
    Command2.Visible = True
        Index = Index + 1
      If Index = 1 Then EndTime = (Timer + 8 * 60) Mod 86400
      Timer1.Interval = 20
    End Sub
    
    Private Sub Command2_Click()
    Timer1.Enabled = False
    Command2.Visible = False
    Command1.Visible = True
    End Sub
    
    Private Sub Form_Load()
        Command1.Caption = "Start"
        Command2.Caption = "Stop"
        Command2.Visible = False
    End Sub
    
    Private Sub Timer1_Timer()
    Dim CtDn As Long
      CtDn = (EndTime - Timer) * 100
      If CtDn < 0 Then
        If CtDn < -1000 Then 'bit of faff to avoid over midnight countdown
          CtDn = CtDn + 8640000
        Else
          Label1.Caption = "0:00:00"
          Beep 'or somesuch
          Timer1.Interval = 0
          Exit Sub
        End If
      End If
      Label1.Caption = Format((CtDn \ 6000) Mod 60, "0:") & Format((CtDn \ 100) Mod 60, "00:") & Format(CtDn Mod 100, "00")
    End Sub

  22. #22

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    43

    Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds

    thanks a lot!

  23. #23
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds

    You betcha!

    Let me know if you need any more assistance.

  24. #24
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Countdown Timer with minutes seconds and milliseconds

    Quote Originally Posted by RhinoBull
    Add Label control,Command button and Timer to your form first, then copy/paste and run this sample code:
    Code:
    Option Explicit
    
    Dim lSeconds As Long
    
    Private Sub Form_Load()
        lSeconds = 8 * 60000
        Label1.Caption = "00:80:00.000"
        Timer1.Interval = 1 '1/1000 of a sec
        Timer1.Enabled = False
    End Sub
    
    Private Sub Command1_Click()
        Timer1.Enabled = Not Timer1.Enabled
    End Sub
    
    Private Sub Timer1_Timer()
    Dim timeleft As String
    
        lSeconds = lSeconds - 1
        
        timeleft = Format((lSeconds \ 3600000), "00") & ":" & _
                  Format((lSeconds Mod 3600000) \ 60000, "00") & ":" & _
                  Format((lSeconds Mod 60000) / 1000, "##.000")
        
        Label1.Caption = timeleft
        If lSeconds = 0 Then Timer1.Enabled = False
    
    End Sub
    Quote Originally Posted by Zach_VB6
    Final:
    Code:
    Option Explicit
    
    Dim lSeconds As Long
    
    Private Sub Form_Load()
        lSeconds = 8 * 60000
        Label1.Caption = "00:08:00.00"
        Timer1.Enabled = False
        Timer1.Interval = 1 '1/1000 of a sec
        Me.Caption = "StopWatch!"
    End Sub
    
    Private Sub Command1_Click()
        Timer1.Enabled = Not Timer1.Enabled
            If Timer1.Enabled = True Then Command1.Caption = "Stop"
            If Timer1.Enabled = False Then Command1.Caption = "Start"
    End Sub
    
    Private Sub Timer1_Timer()
    Dim timeleft As String
    
        lSeconds = lSeconds - 1
        
        timeleft = Format((lSeconds \ 3600000), "00") & ":" & _
                  Format((lSeconds Mod 3600000) \ 60000, "00") & ":" & _
                  Format((lSeconds Mod 60000) / 1000, "##.00")
        
        Label1.Caption = timeleft
        If lSeconds = 0 Then Timer1.Enabled = False
    
    End Sub
    Quote Originally Posted by Zach_VB6
    You betcha!

    Let me know if you need any more assistance.
    Let me know if Zach_VB6 and RhinoBull are the same person?!?!?!
    What's the point? I don't understand!

    @theonlyhugeg, you should not send PMs to Milk and me. Zach_VB6 is the best expert who can help you to do anything you want.

  25. #25

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    43

    Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds

    alright i'll keep it in mind in the future.
    i have accomplished my task however and i dont think i'll be asking anymore questions about countdown timers.
    thanks for your help
    and i dont know if Zach_VB6 and RhinoBull are the same person...

  26. #26
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Countdown Timer with minutes seconds and milliseconds

    Quote Originally Posted by anhn
    Let me know if Zach_VB6 and RhinoBull are the same person?!?!?!
    What's the point? I don't understand!
    No we are not!

    Quote Originally Posted by anhn
    ... Zach_VB6 is the best expert who can help you to do anything you want.
    With all due respect but I'm afraid not.

  27. #27
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds

    I agree totally.

    I am not an "expert".

  28. #28
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    282

    Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds

    How to make normal stopwatch timer from this example, not running backwards?
    HH:mm:ss:sss

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