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.
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.
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.
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
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:
'check autostep seconds
If Not (txtAutoStop.Text = String.Empty) AndAlso (Integer.TryParse(txtAutoStop.Text, AutoStop)) Then
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.
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.
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
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:
Private Watch As New Diagnostics.Stopwatch
Private theTimeSpan As TimeSpan
Private nextSoundTime As Date = DateTime.Now.AddSeconds(1)
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 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
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
'check autostep seconds if empty, if yes don't play sound
If Not (txtAutoStop.Text = String.Empty) AndAlso (Integer.TryParse(txtAutoStop.Text, AutoStop)) Then
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.
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.
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.
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.