Results 1 to 35 of 35

Thread: [RE-OPEN] Stop Watch

  1. #1

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    [RE-OPEN] Stop Watch

    Hi! I've seen the code of jmcilhinney in this Thread. I am using it right now with some minor modification....Right now I want to be able to set the timer to stop say, after 10 seconds or fifteen seconds or any time value stated in my textbox plus I want it to play a tick sound every second and then when it reaches the stop time set it will play another sounds which denotes time is up. It's like a timer for a quiz bee contest....
    Last edited by Simply Me; Feb 4th, 2010 at 07:54 PM.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

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

    Re: Stop Watch

    When you start timing you would create a TimeSpan that represents the amount of time you want to run for, e.g.
    vb.net Code:
    1. Me.totalTime = TimeSpan.FromSeconds(CInt(Mt.TextBox1))
    At each Tick you can then subtract the elasped time from that to determine the amount of time remaining.

    Stopping the Timer and displaying a message at the end is easy. You simply test whether the elapsed time is greater than or equal to the total time and stop if it is.

    Making a sound might be tricky if you're going to allow pausing. If you're not pausing then you simply set the Timer Interval to 1000 and call My.Computer.Audio.Play. If you pause and resume then your Ticks will no longer be on the second so you'd probably have to Tick more often and test whether the elapsed time is within one Interval of the nearest whole second and, if it is, play the sound.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    Here 's what I have right now....I will not be pausing the timer.....
    vb Code:
    1. Public Class FrmStopWatch
    2.     Private watch As New Diagnostics.Stopwatch
    3.  
    4.     Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    5.         Me.watch.Start()
    6.         Me.Timer1.Start()
    7.  
    8.     End Sub
    9.  
    10.     Private Sub Btnstop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
    11.         Me.watch.Stop()
    12.         Me.Timer1.Stop()
    13.     End Sub
    14.  
    15.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    16.         Dim elapsed = Me.watch.Elapsed
    17.         Me.lbltime.Text = String.Format("{0:00}:{1:00}:{2:00}:{3:00}", elapsed.TotalHours, elapsed.Minutes, elapsed.Seconds, elapsed.Milliseconds / 10)
    18.     End Sub
    19.  
    20.     Private Sub BtnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    21.         Me.watch.Reset()
    22.         Me.lbltime.Text = String.Format("00:00:00:00")
    23.     End Sub
    24.  
    25.     Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    26.         Me.Close()
    27.     End Sub
    28. End Class
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  4. #4

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    whenever I add this code
    vb Code:
    1. Me.totalTime = TimeSpan.FromSeconds(CInt(Mt.TextBox1))
    i blue get squiggly line under Mt. I added it under timer1_tick event
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Stop Watch

    Quote Originally Posted by Simply Me View Post
    whenever I add this code
    vb Code:
    1. Me.totalTime = TimeSpan.FromSeconds(CInt(Mt.TextBox1))
    i blue get squiggly line under Mt. I added it under timer1_tick event
    That was supposed to be "Me", not "Mt". That said, you weren't supposed to just copy and paste that code. As I aid in my previous post, it was an example only. You were supposed to read it, understand the principle and then implement that principle in whatever way is appropriate for your project.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    ok here is what I am doing now...but I stil cant get the timer to stop at a certain value I enter in the textbox....
    vb Code:
    1. Public Class FrmStopWatch
    2.     Private watch As New Diagnostics.Stopwatch
    3.     Private theTimeSpan As TimeSpan
    4.     Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    5.         Me.watch.Start()
    6.         Me.Timer1.Start()
    7.     End Sub
    8.  
    9.     Private Sub Btnstop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
    10.         Me.watch.Stop()
    11.         Me.Timer1.Stop()
    12.     End Sub
    13.  
    14.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    15.         Dim elapsed = Me.watch.Elapsed
    16.         Me.lbltime.Text = String.Format("{0:00}:{1:00}:{2:00}:{3:00}", elapsed.TotalHours, elapsed.Minutes, elapsed.Seconds, elapsed.Milliseconds \ 10)
    17.  
    18.         If Me.theTimeSpan.Seconds = CInt(Me.txtautostop.Text) Then
    19.             Timer1.Dispose()
    20.         End If
    21.     End Sub
    22.  
    23.     Private Sub BtnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    24.         Me.watch.Reset()
    25.         Me.lbltime.Text = String.Format("00:00:00:00")
    26.     End Sub
    27.  
    28.     Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    29.         Me.Close()
    30.     End Sub
    31. End Class

    What should be used in the timer interval? is it 1 or 100?
    Attached Images Attached Images  
    Last edited by Simply Me; Dec 14th, 2009 at 04:37 AM.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Stop Watch

    Code:
        Dim isRun As Boolean = False
        Dim stpw As New Diagnostics.Stopwatch
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Interval = 100 '1/10 of a second
        End Sub
        Private Sub btnSS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSS.Click
            'start / stop button
            btnSS.Enabled = False
            If isRun Then 'is the stopwatch running
                isRun = False 'yes
                btnSS.Text = "Start"
                stpw.Stop()
            Else 'no
                isRun = True
                btnSS.Text = "Stop"
                stpw.Reset()
                stpw.Start()
                Timer1.Enabled = True
            End If
            btnSS.Enabled = True
        End Sub
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                        Handles Timer1.Tick
            Dim ts As TimeSpan = stpw.Elapsed
            Dim autoStop As Integer
            'show the stopwatch elapsed time
            tbRT.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", _
                                      ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)
            'check autostep seconds
            If Not (tbAS.Text = String.Empty) AndAlso (Integer.TryParse(tbAS.Text, autoStop)) Then
                If stpw.ElapsedMilliseconds / 1000 >= autoStop Then btnSS.PerformClick()
            Else
                tbAS.Text = String.Empty
            End If
            If Not isRun Then 'if not running disable this event
                Timer1.Enabled = False
            End If
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    Great dbasnett! Thanks!

    how do I add sound for every tick of a second and another sound when the count reaches the value in autostep seconds textbox?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  9. #9

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    I inserted this line of
    vb Code:
    1. My.Computer.Audio.Play(My.Resources.beep_2, AudioPlayMode.WaitToComplete)
    before this
    vb Code:
    1. If stpw.ElapsedMilliseconds / 1000 >= autoStop Then btnSS.PerformClick()
    but its beeping everying milliseconds....I want it to beep every second only
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  10. #10

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    here's what I have now...
    vb Code:
    1. 'check autostep seconds
    2.         If Not (txtAutoStop.Text = String.Empty) AndAlso (Integer.TryParse(txtAutoStop.Text, AutoStop)) Then
    3.             My.Computer.Audio.Play(My.Resources.beep_2, AudioPlayMode.WaitToComplete)
    4.             If Watch.ElapsedMilliseconds / 1000 >= AutoStop Then
    5.                 btnStop.PerformClick()
    6.                 My.Computer.Audio.Play(My.Resources.hohner_melodica_1, AudioPlayMode.WaitToComplete)
    7.             End If
    8.         Else
    9.             txtAutoStop.Text = String.Empty
    10.         End If
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  11. #11

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    ok I made some changes with the code but I cant get the wav file to play every tick of a second....The sound is playing every tick of a millisecond...here's my code now.
    vb Code:
    1. 'check autostep seconds
    2.         If Not (txtAutoStop.Text = String.Empty) AndAlso (Integer.TryParse(txtAutoStop.Text, AutoStop)) Then
    3.             My.Computer.Audio.Play(My.Resources.beep_2, AudioPlayMode.Background)
    4.             If Watch.ElapsedMilliseconds \ 1000 >= AutoStop Then
    5.                 btnStop.PerformClick()
    6.                 My.Computer.Audio.Play(My.Resources.hohner_melodica_1, AudioPlayMode.Background)
    7.             End If
    8.         Else
    9.             txtAutoStop.Text = String.Empty
    10.         End If
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

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

    Re: Stop Watch

    vb.net Code:
    1. Private nextSoundTime As Date
    2.  
    3. Private Sub Timer1_Tick() Handles Timer1.Tick
    4.     If Date.Now >= Me.nextSoundTime Then
    5.         'Play sound here.
    6.  
    7.         Me.nextSoundTime = Me.nextSoundTime.AddSeconds(1)
    8.     End If
    9. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    It still plays the sound every tick of millisecond...
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  14. #14

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    I tired console.beep() but its not working the way I want it.

    Any other ideas how will I set the wav file (beep sound) to play every second and not every millisecond?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Stop Watch

    You have to actually set an initial value for nextSoundTime. My code works as is but you have to set nextSoundTime to the time you want the first sound made when you first start timing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    I tried changing my timer interval to 1000 instead of 100 but the millisecond is not running and the beep sound is not going along with the counting...it's a little bit faster.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  17. #17
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Stop Watch

    using jmc's code

    Code:
    Public Class Form1
        Dim isRun As Boolean = False
        Dim stpw As New Diagnostics.Stopwatch
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Interval = 100 '1/10 of a second
            Timer1.Start()
        End Sub
        Private Sub btnSS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSS.Click
            'start / stop button
            btnSS.Enabled = False
            If isRun Then 'is the stopwatch running
                isRun = False 'yes
                btnSS.Text = "Start"
                stpw.Stop()
            Else 'no
                isRun = True
                btnSS.Text = "Stop"
                stpw.Reset()
                stpw.Start()
                nextStm = DateTime.Now.AddSeconds(1)
                Timer1.Enabled = True
            End If
            btnSS.Enabled = True
        End Sub
        Dim nextStm As DateTime = DateTime.Now.AddSeconds(1)
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                        Handles Timer1.Tick
            Dim ts As TimeSpan = stpw.Elapsed
            Dim autoStop As Integer
            If DateTime.Now >= nextStm Then
                nextStm = nextStm.AddSeconds(1)
                Beep()
            End If
            'show the stopwatch elapsed time
            tbRT.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", _
                                      ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)
            'check autostep seconds
            If Not (tbAS.Text = String.Empty) AndAlso (Integer.TryParse(tbAS.Text, autoStop)) Then
                If stpw.ElapsedMilliseconds / 1000 >= autoStop Then btnSS.PerformClick()
            Else
                tbAS.Text = String.Empty
            End If
            If Not isRun Then 'if not running disable this event
                Timer1.Enabled = False '<<<<<<<<<<<<<<<<<<
            End If
        End Sub
    End Class
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  18. #18

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    i will try it jm.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  19. #19
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Stop Watch

    We posted at the same time. See post #17
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  20. #20

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    I have seen it dbasnett....I am working at it now...thanks
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  21. #21

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    I can get the sound play every second now, but sometimes when I run it, on the first 1 second it plays the sound at .9 millisecond before it will display 1 sec. and it the process it pauses the counting for a very very short period but noticeable. Why is this so? I combine
    The code now looks like this now.
    vb Code:
    1. Private Watch As New Diagnostics.Stopwatch
    2.     Private theTimeSpan As TimeSpan
    3.     Private nextSoundTime As Date = DateTime.Now.AddSeconds(1)
    4.  
    5.     Private Sub FrmStopWatch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Me.Timer1.Interval = 100 '1/10 of a second
    7.     End Sub
    8.  
    9.     Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    10.         Me.Watch.Start()
    11.         Me.Timer1.Start()
    12.  
    13.         nextSoundTime = DateTime.Now.AddSeconds(1)
    14.  
    15.         btnReset.Enabled = False
    16.         btnExit.Enabled = False
    17.     End Sub
    18.  
    19.     Private Sub Btnstop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
    20.         Me.Watch.Stop()
    21.         Me.Timer1.Stop()
    22.  
    23.         btnReset.Enabled = True
    24.         btnExit.Enabled = True
    25.     End Sub
    26.  
    27.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    28.         Dim Elapsed = Me.watch.Elapsed
    29.         Dim AutoStop As Integer
    30.  
    31.         'check autostep seconds if empty, if yes don't play sound
    32.         If Not (txtAutoStop.Text = String.Empty) AndAlso (Integer.TryParse(txtAutoStop.Text, AutoStop)) Then
    33.             If DateTime.Now >= Me.nextSoundTime Then
    34.                 Me.nextSoundTime = Me.nextSoundTime.AddSeconds(1)
    35.                 My.Computer.Audio.Play(My.Resources.GLASSBEL, AudioPlayMode.Background)
    36.             End If
    37.         End If
    38.  
    39.         'show the stopwatch elapsed time
    40.         Me.lbltime.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:0}", _
    41.             Elapsed.TotalHours, Elapsed.Minutes, Elapsed.Seconds, Elapsed.Milliseconds \ 100)
    42.  
    43.         'check autostep seconds
    44.         If Not (txtAutoStop.Text = String.Empty) AndAlso (Integer.TryParse(txtAutoStop.Text, AutoStop)) Then
    45.             If Watch.ElapsedMilliseconds \ 1000>= AutoStop Then
    46.                 btnStop.PerformClick()
    47.                 My.Computer.Audio.Play(My.Resources.FIREALRM, AudioPlayMode.Background)
    48.             End If
    49.         Else
    50.             txtAutoStop.Text = String.Empty
    51.         End If
    52.  
    53.     End Sub
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  22. #22

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Stop Watch

    its okay now.... I just interchange these two lines
    vb Code:
    1. Me.nextSoundTime = Me.nextSoundTime.AddSeconds(1)
    2.                 My.Computer.Audio.Play(My.Resources.GLASSBEL, AudioPlayMode.Background)
    like this
    vb Code:
    1. My.Computer.Audio.Play(My.Resources.GLASSBEL, AudioPlayMode.Background)
    2.                 Me.nextSoundTime = Me.nextSoundTime.AddSeconds(1)

    Many thanks jm and dbasnett!!
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  23. #23

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [RE-OPEN] [RESOLVED] Stop Watch

    Hi! I re-open this thread because I am having trouble with the timer the first time I will run the program, the timer runs fast specifically the timing from 1 second to 2 seconds....but if I will run it again the problem is not occuring....but if I will close the program and then re-run it by then the same problem occurs.....why is this so?

    Here's my code so far.
    Code:
      
        Private Watch As New Diagnostics.Stopwatch
        Private nextSoundTime As Date = DateTime.Now.AddSeconds(1)
        Dim AutoStop As Integer
    
        Private Sub FrmStopWatch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Timer1.Interval = 100 '1/10 of a second
        End Sub
     
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim Elapsed = Me.Watch.Elapsed
            'Dim AutoStop As Integer
    
            'show the stopwatch elapsed time
            Me.lbltime.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:0}", _
                Elapsed.TotalHours, Elapsed.Minutes, Elapsed.Seconds, Elapsed.Milliseconds \ 100)
    
            'check autostep seconds if empty, if yes don't play sound
            If Not (txtAutoStop.Text = String.Empty) AndAlso (Integer.TryParse(txtAutoStop.Text, AutoStop)) Then
                If DateTime.Now >= Me.nextSoundTime Then
                    My.Computer.Audio.Play(My.Resources.BIP, AudioPlayMode.Background)
                    Me.nextSoundTime = Me.nextSoundTime.AddSeconds(1)
                End If
            End If
    
            'check autostep seconds
            If Not (txtAutoStop.Text = String.Empty) AndAlso (Integer.TryParse(txtAutoStop.Text, AutoStop)) Then
                If Watch.ElapsedMilliseconds \ 1000 >= AutoStop Then
                    btnStop.PerformClick()
                    My.Computer.Audio.Play(My.Resources.Piano_PAd_glissando_descend_BLASTWAVEFX_06741, AudioPlayMode.Background)
                    btnReset.Focus()
                End If
            Else
                txtAutoStop.Text = String.Empty
            End If
    
        End Sub
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  24. #24

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [RE-OPEN] [RESOLVED] Stop Watch

    anyone who has idea why this is happening? does it have something to do with the computer I am using?

    I am using intel core 2 duo HP pavilion with 4GB RAM.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  25. #25

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [RE-OPEN] Stop Watch

    Anyone can help me out here please? Everytime I run my project right after I open VB, the timer runs fast specifically the timing from 1 second to 2 seconds.

    This is what I just put a value in my textbox and then run it....The next time I run the app (without closing VB program) then the problem will not occur but the moment I close VB and re-open it and run the project then the same problem occurs....Why is this so?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  26. #26

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [RE-OPEN] Stop Watch

    anyone here please? I am trying to attached the whole project but it exceeds the limit of 500KB, the sound file is too large.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  27. #27
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: [RE-OPEN] Stop Watch

    sounds to me that the timer has started to run before the app has fully loaded
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  28. #28

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [RE-OPEN] Stop Watch

    The timer starts only on click of a button. my code is in post #23
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  29. #29

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [RE-OPEN] Stop Watch

    anybody can shed light on this please?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  30. #30
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: [RE-OPEN] Stop Watch

    In your post #23 there is no command to start the timer.
    The timer starts only after invoking the Start method. Wherever it occurs the timer starts.

  31. #31

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [RE-OPEN] Stop Watch

    Sorry for that....here it is...
    Code:
        Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            Me.Watch.Start()
            Me.Timer1.Start()
    
            nextSoundTime = DateTime.Now.AddSeconds(1)
    
            btnReset.Enabled = False
            btnExit.Enabled = False
        End Sub
    
        Private Sub Btnstop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
            Me.Watch.Stop()
            Me.Timer1.Stop()
    
            btnReset.Enabled = True
            btnExit.Enabled = True
        End Sub
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  32. #32

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [RE-OPEN] Stop Watch

    Still nobody can help me out
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  33. #33
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RE-OPEN] Stop Watch

    Read your first post simplyme. Is that what you are trying to do?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  34. #34

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [RE-OPEN] Stop Watch

    Quote Originally Posted by dbasnett View Post
    Read your first post simplyme. Is that what you are trying to do?
    Yes...I am doing a timer like the one used in quiz bee.....Actually it's already running the only problem is the timing as i've mentioned...there is some kind of "pause" between 1 and 2 seconds which in turn will result to a simultaneous playing of sound (beep sound).
    Last edited by Simply Me; Feb 11th, 2010 at 07:16 PM.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  35. #35

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [RE-OPEN] Stop Watch

    still no one can help me out?
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

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