Results 1 to 17 of 17

Thread: [RESOLVED] Help me with this code.

  1. #1

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Resolved [RESOLVED] Help me with this code.

    Hi!
    I created a stopwatch and it is having some problems: when the stopwatch resets and then the start button is pressed again, the laps continue from the last lap and the stopwatch continues from the last time it was at (ex. start button pressed, lap button pressed at lap 1 = 0:0:50, stop button pressed at 0:1:0, press restart button, press start button (starts from 0:1:00), press lap button at lap 2 = 0:1:50.) and the second problem is that the seconds go way too fast.
    Start1 is the start button.
    Label1 is the label that shows the time.
    Timer1 is obviously a timer.
    Lap is the lap button.
    StopBtn is the stop button.
    Listbox1 is the listbox that lists the laps.
    RestartBtn1 is the restart button.
    Name:  Capture1.JPG
Views: 566
Size:  17.0 KB
    Here is the code:
    Code:
     Dim running As Boolean = False
        Dim seconds As Integer = 0, minutes As Integer = 0, hours As Integer = 0, lapCount As Integer = 0
        Private Sub Start1_Click(sender As Object, e As EventArgs) Handles Start1.Click
            running = True
            Timer1.Enabled = True
            Timer1.Start()
        End Sub
        Private Function updateControls() As UserControl()
            Label1.Text = hours & ":" & minutes & ":" & seconds
        End Function
        Private Sub Lap_Click(sender As Object, e As EventArgs) Handles Lap.Click
            lapCount += 1
            ListBox1.Items.Add("Lap " & lapCount & " - " & hours & ":" & minutes & ":" & seconds)
        End Sub
        Private Sub StopBtn_Click(sender As Object, e As EventArgs) Handles StopBtn.Click
            running = False
            Timer1.Stop()
            Timer1.Enabled = False
        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            seconds += 1
            If (seconds = 60) Then
                seconds = 0
                minutes += 1
                If (minutes = 60) Then
                    minutes = 0
                    hours += 1
                End If
            End If
            updateControls()
        End Sub
        Private Sub RestartBtn1_Click(sender As Object, e As EventArgs) Handles RestartBtn1.Click
            Label1.Text = ""
            ListBox1.Items.Clear()
            Timer1.Enabled = False
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Help me with this code.

    Is this the same topic as the other thread I just posted to moments ago or something different? It's hard to tell because it has basically the same meaningless title and looks related but might be a different specific issue. If it is the same topic, why is it here in another thread?

  3. #3

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    It is not the same thing (I should change the title).

  4. #4

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    This is a stopwatch, the other one is a timer.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Help me with this code.

    You should definitely use a more meaningful and descriptive title for your threads. Pretty much all threads here are asking for help with code so using that as a title tells us very little and may even make some members not bother to look at the thread at all.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Help me with this code.

    A stopwatch and a countdown timer are basically the same thing. You simply use a Stopwatch object to measure the elapsed time. The difference is that, with a countdown, you subtract that elapsed time from the total time you want it to run to find the time remaining.

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Help me with this code.

    Quote Originally Posted by Cow124 View Post
    Hi!
    I created a stopwatch and it is having some problems: when the stopwatch resets and then the start button is pressed again, the laps continue from the last lap and the stopwatch continues from the last time it was at (ex. start button pressed, lap button pressed at lap 1 = 0:0:50, stop button pressed at 0:1:0, press restart button, press start button (starts from 0:1:00), press lap button at lap 2 = 0:1:50.) and the second problem is that the seconds go way too fast.
    Start1 is the start button.
    Label1 is the label that shows the time.
    Timer1 is obviously a timer.
    Lap is the lap button.
    StopBtn is the stop button.
    Listbox1 is the listbox that lists the laps.
    RestartBtn1 is the restart button.
    Name:  Capture1.JPG
Views: 566
Size:  17.0 KB
    Here is the code:
    Code:
     Dim running As Boolean = False
        Dim seconds As Integer = 0, minutes As Integer = 0, hours As Integer = 0, lapCount As Integer = 0
        Private Sub Start1_Click(sender As Object, e As EventArgs) Handles Start1.Click
            running = True
            Timer1.Enabled = True
            Timer1.Start()
        End Sub
        Private Function updateControls() As UserControl()
            Label1.Text = hours & ":" & minutes & ":" & seconds
        End Function
        Private Sub Lap_Click(sender As Object, e As EventArgs) Handles Lap.Click
            lapCount += 1
            ListBox1.Items.Add("Lap " & lapCount & " - " & hours & ":" & minutes & ":" & seconds)
        End Sub
        Private Sub StopBtn_Click(sender As Object, e As EventArgs) Handles StopBtn.Click
            running = False
            Timer1.Stop()
            Timer1.Enabled = False
        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            seconds += 1
            If (seconds = 60) Then
                seconds = 0
                minutes += 1
                If (minutes = 60) Then
                    minutes = 0
                    hours += 1
                End If
            End If
            updateControls()
        End Sub
        Private Sub RestartBtn1_Click(sender As Object, e As EventArgs) Handles RestartBtn1.Click
            Label1.Text = ""
            ListBox1.Items.Clear()
            Timer1.Enabled = False
        End Sub
    Hi, here are some tips that can help you improve your code:
    1. Your seconds are going too fast because you probably have your timer's interval set too low. Is it set to 1000?
    2. The "updateControls" doens't need to be a "function" procedure. You can use a "sub" procedure instead.
    3. Also using the "PadLeft" function as in the following code:
    Code:
    Label1.Text = CStr(hours).PadLeft(2, "0"c) & ":" & CStr(minutes).PadLeft(2, "0"c) & ":" & CStr(seconds).PadLeft(2, "0"c)
    Will neatly pad the numbers used to display the time in the label.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Help me with this code.

    This bit of code is diabolical:
    Code:
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            seconds += 1
            If (seconds = 60) Then
                seconds = 0
                minutes += 1
                If (minutes = 60) Then
                    minutes = 0
                    hours += 1
                End If
            End If
            updateControls()
        End Sub
    If you want to measure time then measure time. Don't count. If you want to create a stopwatch application then logic suggests that using the Stopwatch class is a requirement. You simply Start it and then get the Elapsed time whenever you want. The Interval of the Timer is only an issue for how often you want to update what the user sees because it has absolutely zero effect on the passing of time.

  9. #9

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    Quote Originally Posted by jmcilhinney View Post
    This bit of code is diabolical:
    Code:
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            seconds += 1
            If (seconds = 60) Then
                seconds = 0
                minutes += 1
                If (minutes = 60) Then
                    minutes = 0
                    hours += 1
                End If
            End If
            updateControls()
        End Sub
    If you want to measure time then measure time. Don't count. If you want to create a stopwatch application then logic suggests that using the Stopwatch class is a requirement. You simply Start it and then get the Elapsed time whenever you want. The Interval of the Timer is only an issue for how often you want to update what the user sees because it has absolutely zero effect on the passing of time.
    How would I use the stopwatch class in my code (I am not familiar with it and nice dog)?
    Would I use something like: Label1.Text = CStr(Now), or Label1.Text = DateTime.Now.ToString()
    Here is my new code:
    Code:
     Dim running As Boolean = False
        Dim seconds As Integer = 0, minutes As Integer = 0, hours As Integer = 0, lapCount As Integer = 0
        Private Sub Start1_Click(sender As Object, e As EventArgs) Handles Start1.Click
            running = True
            Timer1.Enabled = True
            Timer1.Start()
        End Sub
        Private Sub updateControls()
            Label1.Text = CStr(hours).PadLeft(2, "0"c) & ":" & CStr(minutes).PadLeft(2, "0"c) & ":" & CStr(seconds).PadLeft(2, "0"c)
        End Sub
        Private Sub Lap_Click(sender As Object, e As EventArgs) Handles Lap.Click
            lapCount += 1
            ListBox1.Items.Add("Lap " & lapCount & " - " & hours & ":" & minutes & ":" & seconds)
        End Sub
        Private Sub StopBtn_Click(sender As Object, e As EventArgs) Handles StopBtn.Click
            running = False
            Timer1.Stop()
            Timer1.Enabled = False
        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            seconds += 1
            If (seconds = 60) Then
                seconds = 0
                minutes += 1
                If (minutes = 60) Then
                    minutes = 0
                    hours += 1
                End If
            End If
            updateControls()
        End Sub
        Private Sub RestartBtn1_Click(sender As Object, e As EventArgs) Handles RestartBtn1.Click
            Label1.Text = ""
            ListBox1.Items.Clear()
            Timer1.Enabled = False
        End Sub

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Help me with this code.

    E.g.
    vb.net Code:
    1. Private watch As New Stopwatch
    2.  
    3. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4.     Me.watch.Start()
    5.     Me.Timer1.Start()
    6. End Sub
    7.  
    8. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    9.     Me.watch.Stop()
    10.     Me.Timer1.Stop()
    11. End Sub
    12.  
    13. Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    14.     Me.Label1.Text = Me.watch.Elapsed.ToString("m\:ss\.fff")
    15. End Sub
    By the way, in future, if someone suggests a type or a member and you're unfamiliar with it, the very first thing you should be doing is using your Help menu to open the documentation and read about that type or member.

  11. #11

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    Thx jmcilhinney a lot my original problem is solved, but now I have another problem. The milliseconds are staying at 997, 998 or 999, but not really doing anything else.
    Code:
     Private watch As New Stopwatch
    
        Private Sub Start1_Click(sender As Object, e As EventArgs) Handles Start1.Click
            Me.watch.Start()
            Me.Timer1.Start()
        End Sub
    
        Private Sub StopBtn_Click(sender As Object, e As EventArgs) Handles StopBtn.Click
            Me.watch.Stop()
            Me.Timer1.Stop()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Me.Label1.Text = Me.watch.Elapsed.ToString("m\:ss\.fff")
        End Sub
        Private Sub Lap_Click(sender As Object, e As EventArgs) Handles Lap.Click
            ListBox1.Items.Add(Label1.Text)
        End Sub
        Private Sub RestartBtn1_Click(sender As Object, e As EventArgs) Handles RestartBtn1.Click
            Me.watch.Reset()
            Label1.Text = "00:00:00:000"
            ListBox1.Items.Clear()
        End Sub

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Help me with this code.

    Quote Originally Posted by Cow124 View Post
    Thx jmcilhinney a lot my original problem is solved, but now I have another problem. The milliseconds are staying at 997, 998 or 999, but not really doing anything else.
    What's the Interval of your Timer? If it's 1000 then, in theory, you're only refreshing every one second so the milliseconds should stay the same. Even if you could refresh every millisecond it would not be visible to the user but a smaller and irregular Interval might be a good idea, e.g. 87. Refreshing the time every ~87 milliseconds will look relatively smooth and natural to the user. It's more than 10 updates per second so they will barely be able to make out the numbers anyway. You should update the display immediately after stopping so that you ensure that it's as up-to-date as possible.

  13. #13

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    Quote Originally Posted by jmcilhinney View Post
    What's the Interval of your Timer? If it's 1000 then, in theory, you're only refreshing every one second so the milliseconds should stay the same. Even if you could refresh every millisecond it would not be visible to the user but a smaller and irregular Interval might be a good idea, e.g. 87. Refreshing the time every ~87 milliseconds will look relatively smooth and natural to the user. It's more than 10 updates per second so they will barely be able to make out the numbers anyway. You should update the display immediately after stopping so that you ensure that it's as up-to-date as possible.
    If I want a very accurate stopwatch, should I put the timer interval to 1?
    and should I set it to an irregular interval like 87 or a more regular interval such as 90?
    Last edited by Cow124; Aug 28th, 2014 at 10:34 AM.

  14. #14
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Help me with this code.

    No you should never set a timer interval to 1

    I would not set it to anything under 50, generally a setting of 100 or 200 is fine.

  15. #15

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    Try to resolve my other problem, this thread is resolved.
    http://www.vbforums.com/showthread.p...with-this-code

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Help me with this code.

    Quote Originally Posted by Cow124 View Post
    If I want a very accurate stopwatch, should I put the timer interval to 1?
    and should I set it to an irregular interval like 87 or a more regular interval such as 90?
    Think about it. How exactly would that help? Your stopwatch already is accurate because it's the Stopwatch object that's doing the timing and that's accurate to beyond the millisecond. It's only the display that lags slightly behind but what human being could follow a display that was accurate to the millisecond anyway? The third decimal place would be nothing more than a blur and the second would be a slightly less blurry blur too.

    An Interval like I suggested will give a sufficient impression of time passing and, if you do as I said and get the elapsed time when you stop timing then what you finally show the user will be completely accurate and that's what actually matters.

    You can use whatever Interval you like but the point of using something like 87 instead of 90 is so that you ensure that the third decimal place changes regularly. If you use 90 and the Timer is accurate then your last decimal place will use the same value every time.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Help me with this code.

    Quote Originally Posted by Cow124 View Post
    Try to resolve my other problem, this thread is resolved.
    http://www.vbforums.com/showthread.p...with-this-code
    As I've already told you, a countdown timer is almost exactly the same as a stopwatch. You still use a Stopwatch to measure the passing of time and then you simply subtract the elapsed time from the total time to get the time remaining. For instance, if you want to countdown 10 minutes then you'd use:
    Code:
    Dim totalTime = TimeSpan.FromMinutes(10)
    To get the time remaining at any point, you do this:
    Code:
    Dim timeRemaining = totalTime - myStopwatch.Elapsed
    Displaying the time remaining is then just a matter of formatting a TimeSpan, as it is for displaying the elapsed time of a stopwatch.

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