Timer question: Scoreboard

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 42
Reputation: ChadW is an unknown quantity at this point 
Solved Threads: 0
ChadW's Avatar
ChadW ChadW is offline Offline
Light Poster

Timer question: Scoreboard

 
0
  #1
Oct 7th, 2006
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 (MMS.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.
Last edited by ChadW; Oct 7th, 2006 at 12:54 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Timer question: Scoreboard

 
0
  #2
Oct 7th, 2006
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 42
Reputation: ChadW is an unknown quantity at this point 
Solved Threads: 0
ChadW's Avatar
ChadW ChadW is offline Offline
Light Poster

Re: Timer question: Scoreboard

 
0
  #3
Oct 7th, 2006
WaltP: Thanks for the quick reply. To answer your question, no, I'm really not sure where to start.
Last edited by ChadW; Oct 7th, 2006 at 9:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Timer question: Scoreboard

 
0
  #4
Oct 7th, 2006
A Timer is a control, just like a button, or an option button or some other fun little tool.... here is a little article: http://www.vbdotnetheaven.com/Code/Jun2003/2084.asp You can have it run a function at set intervals....
Last edited by Comatose; Oct 7th, 2006 at 9:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 42
Reputation: ChadW is an unknown quantity at this point 
Solved Threads: 0
ChadW's Avatar
ChadW ChadW is offline Offline
Light Poster

Re: Timer question: Scoreboard

 
0
  #5
Oct 13th, 2006
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?
Last edited by ChadW; Oct 13th, 2006 at 9:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 36
Reputation: sendoshin is an unknown quantity at this point 
Solved Threads: 1
sendoshin sendoshin is offline Offline
Light Poster

Re: Timer question: Scoreboard

 
0
  #6
Oct 19th, 2006
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:

  1. Private CountDownStart
  2.  
  3. Private Sub Command1_Click()
  4. If Not Timer1.Enabled Then
  5. CountDownStart = Timer
  6. Timer1.Enabled = True
  7. Command1.Caption = "Stop"
  8. Command2.Caption = "Pause"
  9. Command2.Enabled = True
  10. Else
  11. Timer1.Enabled = False
  12. MsgBox "Final Time: " & Label1.Caption & vbCrLf & vbCrLf & "Click OK to reset the clock."
  13. Label1 = "20:00.0"
  14. Command1.Caption = "Start"
  15. Command2.Caption = "-----"
  16. Command2.Enabled = False
  17. End If
  18. End Sub
  19.  
  20. Private Sub Command2_Click()
  21. Static PauseInterval
  22. If Timer1.Enabled Then
  23. PauseInterval = Timer - CountDownStart
  24. Timer1.Enabled = False
  25. Command1.Caption = "Restart"
  26. Command2.Caption = "Resume"
  27. Else
  28. CountDownStart = Timer - PauseInterval
  29. Timer1.Enabled = True
  30. Command1.Caption = "Stop"
  31. Command2.Caption = "Pause"
  32. End If
  33. End Sub
  34.  
  35. Private Sub Timer1_Timer()
  36. TimeDiff = (12000) - Int((Timer - CountDownStart) * 10)
  37. If TimeDiff >= 0 Then
  38. TenthDiff = TimeDiff Mod 10
  39. SecDiff = Int(TimeDiff / 10) Mod 60
  40. MinDiff = Int(TimeDiff / 600)
  41. Label1 = Format(MinDiff, "00") & ":" & Format(SecDiff, "00") & "." & Format(TenthDiff, "0")
  42. Else
  43. Label1 = "00:00.0"
  44. Timer1.Enabled = False
  45. MsgBox "!!!TIME!!!"
  46. Command1.Caption = "Start"
  47. Command2.Caption = "-----"
  48. Command2.Enabled = False
  49. End If
  50. DoEvents
  51. 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 42
Reputation: ChadW is an unknown quantity at this point 
Solved Threads: 0
ChadW's Avatar
ChadW ChadW is offline Offline
Light Poster

Re: Timer question: Scoreboard

 
0
  #7
Oct 23rd, 2006
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?
Insert generic signature here
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 36
Reputation: sendoshin is an unknown quantity at this point 
Solved Threads: 1
sendoshin sendoshin is offline Offline
Light Poster

Re: Timer question: Scoreboard

 
0
  #8
Oct 24th, 2006
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():

  1. Dim MinDiff
  2. Dim SecDiff
  3. Dim TenthDiff
  4. 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
Last edited by sendoshin; Oct 24th, 2006 at 5:36 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 42
Reputation: ChadW is an unknown quantity at this point 
Solved Threads: 0
ChadW's Avatar
ChadW ChadW is offline Offline
Light Poster

Re: Timer question: Scoreboard

 
0
  #9
Oct 24th, 2006
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.

  1.  
  2. Public Class Form1
  3. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  4. Timer1.Enabled = False
  5. Command1.Text = "Start"
  6. Command2.Text = "Pause"
  7. Label1.Text = "20:00.0"
  8. End Sub
  9. Private CountDownStart
  10. Private Sub Command1_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Command1.Click
  11. If Not Timer1.Enabled Then
  12. CountDownStart = Microsoft.VisualBasic.DateAndTime.Timer
  13. Timer1.Enabled = True
  14. Command1.Text = "Stop"
  15. Command2.Text = "Pause"
  16. Command2.Enabled = True
  17. Else
  18. Timer1.Enabled = False
  19. 'MsgBox("Final Time: " & Label1.Text & vbCrLf & vbCrLf & "Click OK to reset the clock.")
  20. Label1.Text = "20:00.0"
  21. Command1.Text = "Start"
  22. Command2.Text = "-----"
  23. Command2.Enabled = False
  24. End If
  25. End Sub
  26. Private Sub Command2_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Command2.Click
  27. Static PauseInterval
  28. If Timer1.Enabled Then
  29. PauseInterval = Microsoft.VisualBasic.DateAndTime.Timer - CountDownStart
  30. Timer1.Enabled = False
  31. Command1.Text = "Restart"
  32. Command2.Text = "Resume"
  33. Else
  34. CountDownStart = Microsoft.VisualBasic.DateAndTime.Timer - PauseInterval
  35. Timer1.Enabled = True
  36. Command1.Text = "Stop"
  37. Command2.Text = "Pause"
  38. End If
  39. End Sub
  40. Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  41. Dim MinDiff
  42. Dim SecDiff
  43. Dim TenthDiff
  44. Dim TimeDiff
  45. TimeDiff = (12000) - Int((Microsoft.VisualBasic.DateAndTime.Timer - CountDownStart) * 10)
  46. If TimeDiff >= 0 Then
  47. TenthDiff = TimeDiff Mod 10
  48. SecDiff = Int(TimeDiff / 10) Mod 60
  49. MinDiff = Int(TimeDiff / 600)
  50. Label1.Text = Format(MinDiff, "00") & ":" & Format(SecDiff, "00") & "." & Format(TenthDiff, "0")
  51. Else
  52. Label1.Text = "00:00.0"
  53. Timer1.Enabled = False
  54. MsgBox("!!!TIME!!!")
  55. Command1.Text = "Start"
  56. Command2.Text = "-----"
  57. Command2.Enabled = False
  58. End If
  59. 'Application.DoEvents()
  60. End Sub
  61. End Class
Insert generic signature here
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 5
Reputation: saldelmundo is an unknown quantity at this point 
Solved Threads: 1
saldelmundo saldelmundo is offline Offline
Newbie Poster

Re: Timer question: Scoreboard

 
0
  #10
May 4th, 2007
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)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC