Hello and thank you for taking time to read my question.
I have been charged with the task of creating a scoreboard for use by one of our local teams, the problem is that I am still very new to visual basic (I am using Visual Basic Express Edition). I have the general layout of how the scoreboard is to look, and many of it's functions I've been able to figure out by the various threads on this site (Thank You Daniweb!). However, one of the main features of the scoreboard is the countdown clock for each period (fairly important, heheh). I'm not sure how to achieve this, though I have seen some threads here that hint at count down timers/stopwatches, the codes listed in those threads don't seem to work with VB express edition or are not what I need for this project. I presume that I would use one of the Timers (not sure of the difference?) and a Label, just not sure they work together.

What I am trying to accomplish is this:
A count down timer that would display as such, 20:00.0 (MM:SS.T) Minutes, Seconds, Tenth of seconds. I would like the be able to assign a keyboard key to start/pause the timer and another key to Reset it to its starting point (i.e. 20 minutes). For example, if the user presses the "A" key on the keyboard the timer begins its countdown, pressing the "A" key again would pause the countdown, and perhaps pressing the "R" key would reset it to 20:00.0

Once again, thank you for your time and consideration of this question from a genuine newbie that is willing to learn.

Recommended Answers

All 10 Replies

Have you been able to get the timer at least working? If so you're almost there. If not, simply make a quick program to understand how the timer works and use a text box to increment and display a count of each time the timer kicks off.

Then just use that timer to count the value down from your starting time. You can use the timer.enable property to start ad stop the timer for time-outs.

WaltP: Thanks for the quick reply. To answer your question, no, I'm really not sure where to start.

Ok, after some experimentation and various searches I have made 'some' progress in how Timers work. I'm still unsure how to code the value of Minute, Second, TenthSecond. I have those objects declared as
Dim Minute as Object
Dim Second as Object
Dim Tsec as Object

but still would like some help as how to tell the code what each of those items are worth (i.e. how do i define a minute, second, tenth of a second) so that the timer acts accordingly?

There are a few ways to do what you're referring to. Perhaps the most reliable method is to save the time you started the countdown in a variable, then calculate the values for the clock display in your code. I made a small example project to help illustrate my answer. I included a Label, a Timer, and two Command Buttons. I didn't name any of them, but the Label displays the current countdown time. The way I set this up, you start the countdown from 20:00.0 using Command1 (the Start/Stop button). You can pause the countdown using Command2 (Pause/Resume). If you stop the countdown by pressing Command1 again, it will reset the timer to 20:00.0. If you pause the countdown (Command2) and then use the Start/Stop button (Command1), it will actually restart the countdown from 20:00.0, which may not be what you expected. Here's the code for what I did:

Private CountDownStart

Private Sub Command1_Click()
    If Not Timer1.Enabled Then
        CountDownStart = Timer
        Timer1.Enabled = True
        Command1.Caption = "Stop"
        Command2.Caption = "Pause"
        Command2.Enabled = True
    Else
        Timer1.Enabled = False
        MsgBox "Final Time: " & Label1.Caption & vbCrLf & vbCrLf & "Click OK to reset the clock."
        Label1 = "20:00.0"
        Command1.Caption = "Start"
        Command2.Caption = "-----"
        Command2.Enabled = False
    End If
End Sub

Private Sub Command2_Click()
    Static PauseInterval
    If Timer1.Enabled Then
        PauseInterval = Timer - CountDownStart
        Timer1.Enabled = False
        Command1.Caption = "Restart"
        Command2.Caption = "Resume"
    Else
        CountDownStart = Timer - PauseInterval
        Timer1.Enabled = True
        Command1.Caption = "Stop"
        Command2.Caption = "Pause"
    End If
End Sub

Private Sub Timer1_Timer()
    TimeDiff = (12000) - Int((Timer - CountDownStart) * 10)
    If TimeDiff >= 0 Then
        TenthDiff = TimeDiff Mod 10
        SecDiff = Int(TimeDiff / 10) Mod 60
        MinDiff = Int(TimeDiff / 600)
        Label1 = Format(MinDiff, "00") & ":" & Format(SecDiff, "00") & "." & Format(TenthDiff, "0")
    Else
        Label1 = "00:00.0"
        Timer1.Enabled = False
        MsgBox "!!!TIME!!!"
        Command1.Caption = "Start"
        Command2.Caption = "-----"
        Command2.Enabled = False
    End If
    DoEvents
End Sub

Tweak that until it fits what you're trying to do with it. The code above is to just give you an idea about one way to do it - likely the most reliable way, as it depends on the system clock to tell it how much time is left rather than counting the number of times the Timer was set off.

Why is it unreliable to just count the number of times that the Timer is set off? Let's say that you are running the above code on a slower machine. Let's say that you want to start another program while the countdown is running - in keeping with your situation, let's say you're opening a Word document to look up the stats of a certain player. Doing this on a slower computer will throw off your timing because the system won't be able to set off the Timer exactly every 10th of a second. The Interval property of the Timer control is just a minimum value. No less than a tenth of a second will pass before the system sets off the Timer again. So imagine the Word document taking all of the processor's time for a full three minutes. You go back to your countdown, and it's three minutes behind!

By checking against the system clock, you can be assured that your timer won't be off, since the system clock doesn't require any software to keep track of the time. It's all done in hardware. That's why the way I did it above is more reliable.

If you have any more questions about this or anything else, feel free to ask. Go well, and may the Winds favor you in your journey.

- Sendoshin

sendoshin: Thank you for your reply, however that code does not work in Visual Basic 2005 Express Edition. I've made as many adjustments/conversions as I was able to (converting .caption to .text, etc), however the following errors are being thrown yet:

Error 1 'Timer' is a type and cannot be used as an expression. (multiple occurances)

Error 4 Name 'TimeDiff' is not declared. (multiple)

Error 7 Name 'TenthDiff' is not declared. (multiple)

Name 'MinDiff' is not declared. (multiple)

Name 'SecDiff' is not declared. (multiple)

Name 'DoEvents is not declared.


Any ideas?

Somehow I got the idea that I was still on the VB4/5/6 board, so I gave you VB6 code. The good news is that the code works flawlessly in VB6 - I tested it before posting. The better news is, I know how to fix it so it works in .NET - even though situations involving my web server have prevented me from actually installing .NET as of yet.

Everywhere it says "Timer" (NOT Timer1), change it to "Microsoft.VisualBasic.DateAndTime.Timer". I'm not sure why they changed that (Timer has been with us since TIMER in QBasic, and likely before), but there it is.

In VB6 you have the option of not declaring your variables before you use them. In VB.NET (or VB6 with Option Explicit at the beginning of the form/module), this is not the case. So, to fix (most of) the other problems you've got, simply add the following lines to the top of Timer1_Timer():

Dim MinDiff
Dim SecDiff
Dim TenthDiff
Dim TimeDiff

And for DoEvents, they moved it into Application: Application.DoEvents() should work the same. Alternately, you might try just removing it altogether. It just ensures that the window gets redrawn every time the Timer1_Timer() event is called.

I apologize for getting lost! If anything else comes up missing or broken, let me know and I'll see what I can do to help you out! Sorry again! Good luck!

- Sendoshin

Works wonderfully now that we have the right syntax. :p
I have a few tweaks I will play with, but otherwise perfect, thanks much!

For the sake of others that may be looking for a countdown or stopwatch timer code, here is the final markup in .NET formatting. This code is set to use two Buttons (Command1 and Command2), 1 Timer (Timer1) and a Label (Label1). Note: There is some 'jumpy-ness' to the countdown due to number spacing/shapes depending on how you align the text within its control area. Font also plays a factor.

Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Timer1.Enabled = False
Command1.Text = "Start"
Command2.Text = "Pause"
Label1.Text = "20:00.0"
End Sub
Private CountDownStart
Private Sub Command1_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Command1.Click
If Not Timer1.Enabled Then
CountDownStart = Microsoft.VisualBasic.DateAndTime.Timer
Timer1.Enabled = True
Command1.Text = "Stop"
Command2.Text = "Pause"
Command2.Enabled = True
Else
Timer1.Enabled = False
'MsgBox("Final Time: " & Label1.Text & vbCrLf & vbCrLf & "Click OK to reset the clock.")
Label1.Text = "20:00.0"
Command1.Text = "Start"
Command2.Text = "-----"
Command2.Enabled = False
End If
End Sub
Private Sub Command2_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Command2.Click
Static PauseInterval
If Timer1.Enabled Then
PauseInterval = Microsoft.VisualBasic.DateAndTime.Timer - CountDownStart
Timer1.Enabled = False
Command1.Text = "Restart"
Command2.Text = "Resume"
Else
CountDownStart = Microsoft.VisualBasic.DateAndTime.Timer - PauseInterval
Timer1.Enabled = True
Command1.Text = "Stop"
Command2.Text = "Pause"
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim MinDiff
Dim SecDiff
Dim TenthDiff
Dim TimeDiff
TimeDiff = (12000) - Int((Microsoft.VisualBasic.DateAndTime.Timer - CountDownStart) * 10)
If TimeDiff >= 0 Then
TenthDiff = TimeDiff Mod 10
SecDiff = Int(TimeDiff / 10) Mod 60
MinDiff = Int(TimeDiff / 600)
Label1.Text = Format(MinDiff, "00") & ":" & Format(SecDiff, "00") & "." & Format(TenthDiff, "0")
Else
Label1.Text = "00:00.0"
Timer1.Enabled = False
MsgBox("!!!TIME!!!")
Command1.Text = "Start"
Command2.Text = "-----"
Command2.Enabled = False
End If
'Application.DoEvents()
End Sub
End Class

This thread is of great help to my current project. Can someone help me with how to adapt the pause and resume commands to system text files rather than user input buttons> ie. instead of a button, it would execute the pause or resume command based on the content of a text file that says "pause" or "resume" in it (this file is being dropped by another program)

Member Avatar for Dukane

Use a FileSystemWatcher class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.