Results 1 to 7 of 7

Thread: My Stopwatch has gotten out of hand

Threaded View

  1. #1

    Thread Starter
    Member KodeWorrier's Avatar
    Join Date
    Nov 2005
    Location
    Vermont
    Posts
    55

    My Stopwatch has gotten out of hand

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

    VB Code:
    1. ' Why do I have so many variables?
    2. Dim SetStopWatch As Integer ' on off
    3. Dim SaveStopWatchTime As Integer
    4. Dim ChartTime(12, 90) As String ' needed later
    5. Dim StartTime As Date
    6. Dim EndTime As Date
    7. Dim NewTime As Date
    8. Dim SaveRecord As Date
    9. Dim Account As Integer
    10. Dim Placement As Integer
    11. Dim Record As String
    12.  
    13. ' StopWatch (This part seem fine)
    14. Private Sub TimerStopWatch_Timer()
    15.     If SetStopWatch = 1 Then
    16.         EndTime = Time ' Here is the clock
    17.         Record = EndTime - StartTime
    18.         NewTime = EndTime - StartTime
    19.         LabelWatch.Caption = Format(Record, "HH:MM:SS")
    20.     End If
    21. End Sub
    22.  
    23. 'This part is not too bad, but the first 'if' seems like it could be done better.
    24. Private Sub StartButton_Click()
    25.     If EndTime = 0 Then StartTime = Time Else StartTime = Time - NewTime
    26.     SetStopWatch = Toggle(SetStopWatch) 'Toggle() is just "if 0 then 1 else 0"
    27.     If SetStopWatch = 0 Then StartButton.Caption = "Start" Else StartButton.Caption = "Stop"
    28.     If SetStopWatch = 1 Then ResetButton.Enabled = False Else ResetButton.Enabled = True
    29.     If SetStopWatch = 1 Then SaveTimeButton.Enabled = False Else SaveTimeButton.Enabled = True
    30. End Sub
    31.  
    32. 'Seems like this could be done better.
    33. 'I save the record to use if reset is pressed by mistake
    34. 'I clear the string by brute force
    35. 'then reset the end time to force everything to zero
    36. Private Sub ResetButton_Click()
    37.     SaveRecord = Record
    38.     Record = "00:00:00"
    39.     EndTime = 0
    40.     LabelWatch.Caption = Format(Record, "HH:MM:SS")
    41. End Sub
    42.  
    43. Private Sub SaveTimeButton_Click()
    44.     Account = 1 ' fake an account, later there will be an array of accounts
    45.     ' This part stores the record of times, allowing a save even after a reset
    46.     'I would rather do it without the replication of the whole process and without the goto
    47.     If Record = "00:00:00" Then
    48.         If MsgBox("Save the Record before last reset?", vbYesNo) = 7 Then GoTo Drop
    49.         Placement = Placement + 1
    50.         ChartTime(Account, Placement) = Format(SaveRecord, "HH:MM:SS")
    51.         lstRecentRecord.AddItem "Placement " + Str(Placement) + " - " + ChartTime(Account, Placement), 0
    52.     Else
    53.         Placement = Placement + 1
    54.         ChartTime(Account, Placement) = Format(Record, "HH:MM:SS")
    55.         lstRecentRecord.AddItem "Placement " + Str(Placement) + " - " + ChartTime(Account, Placement), 0
    56.     End If
    57. Drop:
    58. End Sub
    Attached Files Attached Files
    Last edited by KodeWorrier; Dec 2nd, 2005 at 12:38 AM.
    The man who sets out to carry a cat by its tail
    learns something that will always be useful and
    which will never grow dim or doubtful. - Mark Twain

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