superblades
asked on
countdown timer in vb
im writing a vb program and i need a way of counting down upon starting the program e.g.
i want a label that says 5:00 min and it counts down 4:59, 4:58 etc. until it reaches zero
how is this possible?
Regards
superblades
i want a label that says 5:00 min and it counts down 4:59, 4:58 etc. until it reaches zero
how is this possible?
Regards
superblades
ASKER
sorry its been a long time since ive even looked at vb, can you give me a run down in idiots guide what to do? im using vb 2008 express edition
Hi, here the code for doing that:
Add a label name lblTimer
Add a Timer name Timer1
followed is the code:
Dim minutes As Integer
Dim seconds As Integer
Dim TotalMinutes As Integer
Private Sub Form_Load()
TotalMinutes = 2
minutes = TotalMinutes
displayTimer
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
decrement
End Sub
Sub decrement()
seconds = seconds - 1
If seconds < 0 Then
minutes = minutes - 1
seconds = 59
End If
If minutes < 0 Then
minutes = TotalMinutes
seconds = 0
End If
displayTimer
End Sub
Sub displayTimer()
Dim displaySecond As String
Dim displayMinute As String
displaySecond = seconds
displayMinute = minutes
If seconds < 10 Then displaySecond = "0" & seconds
If minutes < 10 Then displayMinute = "0" & minutes
lblTimer.Caption = displayMinute & ":" & displaySecond
End Sub
Add a label name lblTimer
Add a Timer name Timer1
followed is the code:
Dim minutes As Integer
Dim seconds As Integer
Dim TotalMinutes As Integer
Private Sub Form_Load()
TotalMinutes = 2
minutes = TotalMinutes
displayTimer
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
decrement
End Sub
Sub decrement()
seconds = seconds - 1
If seconds < 0 Then
minutes = minutes - 1
seconds = 59
End If
If minutes < 0 Then
minutes = TotalMinutes
seconds = 0
End If
displayTimer
End Sub
Sub displayTimer()
Dim displaySecond As String
Dim displayMinute As String
displaySecond = seconds
displayMinute = minutes
If seconds < 10 Then displaySecond = "0" & seconds
If minutes < 10 Then displayMinute = "0" & minutes
lblTimer.Caption = displayMinute & ":" & displaySecond
End Sub
Just change TotalMinutes = 5 for 5 minutes interval.
Or if you just want the timer to stop at zero and stop counting after time out, just change the decrement part to followed:
Sub decrement()
seconds = seconds - 1
If seconds < 0 Then
minutes = minutes - 1
seconds = 59
End If
If minutes < 0 Then
minutes = 0
seconds = 0
Timer1.Enabled = False
'====Do whatever thing you here for timeout====
End If
displayTimer
End Sub
Sub decrement()
seconds = seconds - 1
If seconds < 0 Then
minutes = minutes - 1
seconds = 59
End If
If minutes < 0 Then
minutes = 0
seconds = 0
Timer1.Enabled = False
'====Do whatever thing you here for timeout====
End If
displayTimer
End Sub
ASKER
Error 3 Name 'Timer1' is not declared. C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form 1.vb 55 9 StopShutdown
Error 4 Name 'Timer1' is not declared. C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form 1.vb 56 9 StopShutdown
Error 2 'Public Sub displayTimer()' has multiple definitions with identical signatures. C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form 1.vb 26 9 StopShutdown
Error 1 'Private Sub Timer1_Timer()' has multiple definitions with identical signatures. C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form 1.vb 10 17 StopShutdown
Error 4 Name 'Timer1' is not declared. C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form
Error 2 'Public Sub displayTimer()' has multiple definitions with identical signatures. C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form
Error 1 'Private Sub Timer1_Timer()' has multiple definitions with identical signatures. C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form
Hi, hav u add a timer named timer1, and add a label named lblTimer on your form?
ASKER
Public Class Form1
Dim minutes As Integer
Dim seconds As Integer
Dim TotalMinutes As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
End Sub
Private Sub Timer1_Timer()
decrement()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TotalMinutes = 2
minutes = TotalMinutes
displayTimer()
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Sub decrement()
seconds = seconds - 1
If seconds < 0 Then
minutes = minutes - 1
seconds = 59
End If
If minutes < 0 Then
minutes = TotalMinutes
seconds = 0
End If
displayTimer()
End Sub
Sub displayTimer()
Dim displaySecond As String
Dim displayMinute As String
displaySecond = seconds
displayMinute = minutes
If seconds < 10 Then displaySecond = "0" & seconds
If minutes < 10 Then displayMinute = "0" & minutes
Label3.Text = displayMinute & ":" & displaySecond
End Sub
End Class
Im getting the following messages
Error 1 Name 'Timer1' is not declared. C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form 1.vb 28 9 StopShutdown
Error 2 Name 'Timer1' is not declared. C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form 1.vb 29 9 StopShutdown
anyideas?
Dim minutes As Integer
Dim seconds As Integer
Dim TotalMinutes As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
End Sub
Private Sub Timer1_Timer()
decrement()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TotalMinutes = 2
minutes = TotalMinutes
displayTimer()
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Sub decrement()
seconds = seconds - 1
If seconds < 0 Then
minutes = minutes - 1
seconds = 59
End If
If minutes < 0 Then
minutes = TotalMinutes
seconds = 0
End If
displayTimer()
End Sub
Sub displayTimer()
Dim displaySecond As String
Dim displayMinute As String
displaySecond = seconds
displayMinute = minutes
If seconds < 10 Then displaySecond = "0" & seconds
If minutes < 10 Then displayMinute = "0" & minutes
Label3.Text = displayMinute & ":" & displaySecond
End Sub
End Class
Im getting the following messages
Error 1 Name 'Timer1' is not declared. C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form
Error 2 Name 'Timer1' is not declared. C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form
anyideas?
ASKER
how do i add a timer?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
in the timers tick event update your label with your new time.
pretty simple
good luck
greg