Results 1 to 7 of 7

Thread: My Stopwatch has gotten out of hand

  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

  2. #2

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

    Re: My Stopwatch has gotten out of hand

    OK, I posted this late last night (Way past my bedtime! ) 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 .

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

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: My Stopwatch has gotten out of hand

    Merry Christmas

    Hope you can get something out of my rewrite.

    Attached Files Attached Files

  4. #4

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

    Re: My Stopwatch has gotten out of hand

    Quote Originally Posted by penagate
    Merry Christmas
    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
    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

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: My Stopwatch has gotten out of hand

    Originally posted by KodeWorrior
    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
    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

  6. #6

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

    Re: My Stopwatch has gotten out of hand

    Quote Originally Posted by penagate
    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.

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

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: My Stopwatch has gotten out of hand

    No worries

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