PDA

Click to See Complete Forum and Search --> : My Stopwatch has gotten out of hand


KodeWorrier
Dec 1st, 2005, 11:10 PM
In an attempt to make my stopwatch save it's times and resume, I ended up getting something messy and klunky. I am sure I could have done this with half the variables, half the If's and probably half the lines of text.

Here's the code, I apologize in advance for not using VB standard variables and names. Should I attach the actual files? Maybe I should...

' Why do I have so many variables?
Dim SetStopWatch As Integer ' on off
Dim SaveStopWatchTime As Integer
Dim ChartTime(12, 90) As String ' needed later
Dim StartTime As Date
Dim EndTime As Date
Dim NewTime As Date
Dim SaveRecord As Date
Dim Account As Integer
Dim Placement As Integer
Dim Record As String

' StopWatch (This part seem fine)
Private Sub TimerStopWatch_Timer()
If SetStopWatch = 1 Then
EndTime = Time ' Here is the clock
Record = EndTime - StartTime
NewTime = EndTime - StartTime
LabelWatch.Caption = Format(Record, "HH:MM:SS")
End If
End Sub

'This part is not too bad, but the first 'if' seems like it could be done better.
Private Sub StartButton_Click()
If EndTime = 0 Then StartTime = Time Else StartTime = Time - NewTime
SetStopWatch = Toggle(SetStopWatch) 'Toggle() is just "if 0 then 1 else 0"
If SetStopWatch = 0 Then StartButton.Caption = "Start" Else StartButton.Caption = "Stop"
If SetStopWatch = 1 Then ResetButton.Enabled = False Else ResetButton.Enabled = True
If SetStopWatch = 1 Then SaveTimeButton.Enabled = False Else SaveTimeButton.Enabled = True
End Sub

'Seems like this could be done better.
'I save the record to use if reset is pressed by mistake
'I clear the string by brute force
'then reset the end time to force everything to zero
Private Sub ResetButton_Click()
SaveRecord = Record
Record = "00:00:00"
EndTime = 0
LabelWatch.Caption = Format(Record, "HH:MM:SS")
End Sub

Private Sub SaveTimeButton_Click()
Account = 1 ' fake an account, later there will be an array of accounts
' This part stores the record of times, allowing a save even after a reset
'I would rather do it without the replication of the whole process and without the goto
If Record = "00:00:00" Then
If MsgBox("Save the Record before last reset?", vbYesNo) = 7 Then GoTo Drop
Placement = Placement + 1
ChartTime(Account, Placement) = Format(SaveRecord, "HH:MM:SS")
lstRecentRecord.AddItem "Placement " + Str(Placement) + " - " + ChartTime(Account, Placement), 0
Else
Placement = Placement + 1
ChartTime(Account, Placement) = Format(Record, "HH:MM:SS")
lstRecentRecord.AddItem "Placement " + Str(Placement) + " - " + ChartTime(Account, Placement), 0
End If
Drop:
End Sub

KodeWorrier
Dec 2nd, 2005, 07:20 AM
OK, I posted this late last night (Way past my bedtime! :o ) after messing around for nearly three hours just to make it run by itself. Maybe it's not the sort of problem usually posted here. But I really hope someone takes the time to criticize it (in a good way) and maybe suggest some general tips that will help me set up and fix other more complex programs before I mess them up too badly.

I wrote it as part of another program and it is used to measure a testing sequence. Most of my agony last night was caused by trying to extract a working subset of the original program. Comments were added later, probably they are inadequate. Of course, the basic stopwatch is what several sources suggested, any elegance there is not my fault. The klunky parts, the wasted mental space and the violation of all things good and fair is all me :blush: .

Somehow I get the feeling that there should be two or three variables in two or three subs which I can set and reset to accomplish what is really a trivial job; Start, Stop, Resume, Reset, and Save the stopwatch time.

Thanks in advance for your efforts, and since I will be here at the computer most of the day, any comments or suggestions could be pm'ed or posted and I can respond within a few minutes.

If you can't help, thanks for reading anyway, and have a nice day! :wave: We are getting a light, pretty and Christmas'y snow. I'll be drinking coffee with cocoa in it and I might have a peanut butter cookie later.

penagate
Dec 4th, 2005, 11:32 AM
Merry Christmas :D

Hope you can get something out of my rewrite.

KodeWorrier
Dec 4th, 2005, 01:37 PM
Merry Christmas :D
A very Merry Christmas to you and yours!


Hope you can get something out of my rewrite.

Quite a lot. Thanks!

"Boolean = not Boolean" is a great trick, takes half the steam out of my Toggle() function. I'll be rewriting half my lesson solutions.

IIF() is cool too. That seems like something that should be near the front of my lesson book but it's not even listed in the index or appendix.

Best of all is enabling and disabling the Timer. Sure saves a lot. I assumed that it was something that ran no matter what.

Two things (which I am sure I can figure out) are the disabling of buttons while timing, and saving the reset time for just in case. Was there a design reason you took those out? I was assuming that people will make mistakes if you let them and wanted to prevent or repair those mistakes for them.

But I'm not complaining. I am surprised anyone took the time out of their day to redo this and I am very grateful. I learned a lot that I will be able to use every day. I couldn't be more pleased!

Have a nice day and thanks again.

Bill

penagate
Dec 4th, 2005, 11:55 PM
Originally posted by KodeWorrior (http://www.vbforums.com/showpost.php?p=2266976&postcount=4)
IIF() is cool too. That seems like something that should be near the front of my lesson book but it's not even listed in the index or appendix.
There is one thing you should know about IIf(). It is a function - not a language construct like an If... Then block. That means that it is called like a function - both true and false expressions are evaluated and passed to the function and one of them is returned based on the boolean value. That's fine in most cases, but sometimes you have a choice of two actions and one of those actions will not be valid, based on the condition. In that case you HAVE to use If..Then instead because IIf() will carry out both actions regardless of the condition - so you will get an error on the invalid one.

Originally posted by KodeWorrior (http://www.vbforums.com/showpost.php?p=2266976&postcount=4)
Two things (which I am sure I can figure out) are the disabling of buttons while timing, and saving the reset time for just in case. Was there a design reason you took those out? I was assuming that people will make mistakes if you let them and wanted to prevent or repair those mistakes for them.
I didn't see a reason to disable the buttons - because you can use all of them at any time. And I didn't save the time before resetting because there is no facility to recover it - but you could easily add that. I hope that's what you meant by it :)

KodeWorrier
Dec 5th, 2005, 07:23 AM
Then instead because IIf() will carry out both actions regardless of the condition - so you will get an error on the invalid one.

I get that, only use it if both cases exist and work. :thumb:


I didn't see a reason to disable the buttons - because you can use all of them at any time. And I didn't save the time before resetting because there is no facility to recover it - but you could easily add that. I hope that's what you meant by it :)
Yes, I have already put them back in. The end program is intended to be used by children and to keep records for school, so I don't want them saving before they are done, or forgetting to save when they are done.

Thanks again! :wave:

penagate
Dec 5th, 2005, 07:24 AM
No worries :)