?
Solved

countdown timer in vb

Posted on 2007-08-06
10
Medium Priority
?
6,377 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:superblades
  • 5
  • 4
10 Comments
 
LVL 8

Expert Comment

by:Autoeforms
ID: 19637421
add a timer control to your form and set the interval to 1000 mili seconds. this way it will fire once a second.

in the timers tick event update your label with your new time.

pretty simple

good luck
greg
0
 
LVL 4

Author Comment

by:superblades
ID: 19637513
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
0
 
LVL 9

Expert Comment

by:william007
ID: 19637514
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

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:william007
ID: 19637517
Just change TotalMinutes = 5 for 5 minutes interval.
0
 
LVL 9

Expert Comment

by:william007
ID: 19637543
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
0
 
LVL 4

Author Comment

by:superblades
ID: 19637563
Error      3      Name 'Timer1' is not declared.      C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form1.vb      55      9      StopShutdown
Error      4      Name 'Timer1' is not declared.      C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form1.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\Form1.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\Form1.vb      10      17      StopShutdown
0
 
LVL 9

Expert Comment

by:william007
ID: 19637582
Hi, hav u add a timer named timer1, and add a label named lblTimer on your form?
0
 
LVL 4

Author Comment

by:superblades
ID: 19637583
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\Form1.vb      28      9      StopShutdown
Error      2      Name 'Timer1' is not declared.      C:\Documents and Settings\BT41234\Local Settings\Application Data\Temporary Projects\StopShutdown\Form1.vb      29      9      StopShutdown


anyideas?
0
 
LVL 4

Author Comment

by:superblades
ID: 19637586
how do i add a timer?
0
 
LVL 9

Accepted Solution

by:
william007 earned 2000 total points
ID: 19637629
From the place you add the label, find for "Timer", click on it, and it will be added.

Sorry, previous code I am posting is for vb 6. If you are using vb.net, use the code as followed:

    Dim minutes As Integer
    Dim seconds As Integer
    Dim TotalMinutes As Integer
    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 = 10
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        decrement()
    End Sub

    Private 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
        End If
        displayTimer()
    End Sub

    Private 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.Text = displayMinute & ":" & displaySecond

    End Sub
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question