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
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