Results 1 to 5 of 5

Thread: CountDown Timer

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    CountDown Timer

    Hello
    I need to count down (from 2 hours) the time - in other words count backwards from 1:59:59 to 00:00:00

    This is what I've done so far, can anyone help me improve
    VB Code:
    1. Private Sub ETCountDown()
    2.         Static ETSec As Integer = 60
    3.         Static ETMin As Integer = 59
    4.         Static ETHour As Integer = ETDur - 1
    5.  
    6.         ''''''''''''''''''''''''''''''''''
    7.         'seconds
    8.         ETSec = ETSec - 1
    9.         lblETTimeLeft.Text = CStr(ETHour) & ":" & CStr(ETMin) & ":" & CStr(ETSec)
    10.         If ETSec = 0 Then
    11.             ETMin = ETMin - 1
    12.             lblETTimeLeft.Text = CStr(ETHour) & ":" & CStr(ETMin) & ":00"
    13.             ETSec = 59
    14.         ElseIf ETSec < 10 Then
    15.             lblETTimeLeft.Text = CStr(ETHour) & ":" & CStr(ETMin) & ":0" & CStr(ETSec)
    16.         End If
    17.         '''''''''''''''''''''''''''''''''
    18.  
    19.         ''''''''''''''''''''''''''''''''
    20.         'minutes not too sure!
    21.         If ETMin = 0 Then
    22.             ETHour = ETHour - 1
    23.             lblETTimeLeft.Text = CStr(ETHour) & ":00" & CStr(ETSec)
    24.             ETMin = 59
    25.         ElseIf ETMin < 10 Then
    26.             If ETSec < 10 Then
    27.                 lblETTimeLeft.Text = CStr(ETHour) & ":0" & CStr(ETMin) & ":0" & CStr(ETSec)
    28.             Else
    29.                 lblETTimeLeft.Text = CStr(ETHour) & ":0" & CStr(ETMin) & ":" & CStr(ETSec)
    30.             End If
    31.             End If
    32.         '''''''''''''''''''''''''''''''
    33.  
    34.         '''''''''''''''''''''''''''''''
    35.         'hour busy
    36.         If ETHour = 0 Then
    37.             lblETTimeLeft.Text = "00:" & CStr(ETMin) & ":" & CStr(ETSec)
    38.         ElseIf ETHour < 10 Then
    39.             lblETTimeLeft.Text = "0:" & CStr(ETMin) & ":" & CStr(ETSec)
    40.         End If
    41.     End Sub
    42.  
    43. Private Sub tET_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tET.Tick
    44.         ETCountDown() 'interval is set to 1000
    45.  
    46.     End Sub

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: CountDown Timer

    Wow. 2 for 2. Upgrade Wizard strikes again!
    VB Code:
    1. Option Strict Off
    2. Option Explicit On
    3. Friend Class Form1
    4.     Inherits System.Windows.Forms.Form
    5. #Region "Windows Form Designer generated code "
    6.     Public Sub New()
    7.         MyBase.New()
    8.         If m_vb6FormDefInstance Is Nothing Then
    9.             If m_InitializingDefInstance Then
    10.                 m_vb6FormDefInstance = Me
    11.             Else
    12.                 Try
    13.                     'For the start-up form, the first instance created is the default instance.
    14.                     If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
    15.                         m_vb6FormDefInstance = Me
    16.                     End If
    17.                 Catch
    18.                 End Try
    19.             End If
    20.         End If
    21.         'This call is required by the Windows Form Designer.
    22.         InitializeComponent()
    23.     End Sub
    24.     'Form overrides dispose to clean up the component list.
    25.     Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
    26.         If Disposing Then
    27.             If Not components Is Nothing Then
    28.                 components.Dispose()
    29.             End If
    30.         End If
    31.         MyBase.Dispose(Disposing)
    32.     End Sub
    33.     'Required by the Windows Form Designer
    34.     Private components As System.ComponentModel.IContainer
    35.     Public ToolTip1 As System.Windows.Forms.ToolTip
    36.     Public WithEvents Timer1 As System.Windows.Forms.Timer
    37.     'NOTE: The following procedure is required by the Windows Form Designer
    38.     'It can be modified using the Windows Form Designer.
    39.     'Do not modify it using the code editor.
    40.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    41.         Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
    42.         Me.components = New System.ComponentModel.Container()
    43.         Me.ToolTip1 = New System.Windows.Forms.ToolTip(components)
    44.         Me.ToolTip1.Active = True
    45.         Me.Timer1 = New System.Windows.Forms.Timer(components)
    46.         Me.Text = "Form1"
    47.         Me.ClientSize = New System.Drawing.Size(312, 206)
    48.         Me.Location = New System.Drawing.Point(4, 30)
    49.         Me.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultLocation
    50.         Me.Font = New System.Drawing.Font("Arial", 8!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    51.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    52.         Me.BackColor = System.Drawing.SystemColors.Control
    53.         Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable
    54.         Me.ControlBox = True
    55.         Me.Enabled = True
    56.         Me.KeyPreview = False
    57.         Me.MaximizeBox = True
    58.         Me.MinimizeBox = True
    59.         Me.Cursor = System.Windows.Forms.Cursors.Default
    60.         Me.RightToLeft = System.Windows.Forms.RightToLeft.No
    61.         Me.ShowInTaskbar = True
    62.         Me.HelpButton = False
    63.         Me.WindowState = System.Windows.Forms.FormWindowState.Normal
    64.         Me.Name = "Form1"
    65.         Me.Timer1.Enabled = False
    66.         Me.Timer1.Interval = 1
    67.     End Sub
    68. #End Region
    69. #Region "Upgrade Support "
    70.     Private Shared m_vb6FormDefInstance As Form1
    71.     Private Shared m_InitializingDefInstance As Boolean
    72.     Public Shared Property DefInstance() As Form1
    73.         Get
    74.             If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
    75.                 m_InitializingDefInstance = True
    76.                 m_vb6FormDefInstance = New Form1()
    77.                 m_InitializingDefInstance = False
    78.             End If
    79.             DefInstance = m_vb6FormDefInstance
    80.         End Get
    81.         Set
    82.             m_vb6FormDefInstance = Value
    83.         End Set
    84.     End Property
    85. #End Region
    86.     ' Add a Timer control to your project.  It will be Timer1
    87.     ' It looks like a stop watch in the IDE.
    88.     Dim OldTime As Date
    89.     Dim newTime As Date
    90.     Dim diff As Integer
    91.    
    92.     Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
    93.         OldTime = DateAdd(Microsoft.VisualBasic.DateInterval.Second, 7200, TimeOfDay) ' Add 2 hourss
    94.         Timer1.Interval = 10
    95.         Timer1.Enabled = True
    96.     End Sub
    97.    
    98.     Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick
    99.         newTime = TimeOfDay
    100.         diff = DateDiff(Microsoft.VisualBasic.DateInterval.Second, newTime, OldTime)
    101.         Form1.DefInstance.Text = (diff \ 3600) & ":" & VB6.Format(diff \ 60 Mod 60, "00") & ":" & VB6.Format(diff - ((diff \ 60) * 60), "00")
    102.         If diff = 0 Then
    103.             Timer1.Enabled = False
    104.             ' You time is UP!  Do something!
    105.             Beep()
    106.         End If
    107.     End Sub
    108. End Class

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: CountDown Timer

    hi David!
    Thanx, but I don't need the timeofday - it should exactly countdown from 2 hours only, how can I do it?

  4. #4
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: CountDown Timer

    I created a CountDownTimer control based on the System.Windows.Forms.Timer control to do something like this. I'd give you almost the exact code but it's in C#.

    For the logic of counting down, you'll want some nested If statements like the following:
    VB Code:
    1. If (Seconds = 0) Then
    2. Seconds = 59
    3.        If (Minutes = 0) Then
    4.             Minutes = 59
    5.                   If (Not Hours = 0) Then
    6.                        Hours -= 1
    7.                   End If
    8.        Else
    9.             Minutes -= 1
    10.        End If
    11. Else
    12. Seconds -= 1
    13. End If

    Then you check when all 3 (Hours, Minutes, Seconds) are 0 and then your time is done!

    Very simple

    Now if you want to format it so there is always two digits shown (like, if the timer gets to 3 seconds, you want it to show 03), then make a function to pass each number to. If it's >= 10, don't worry about it. Otherwise, add a 0 to the front.

    Of course what I described is expandable to higher than two hours. A contorl I'm going to publish later can do days. I thought about years but who is there to say how long a year is with leap years involved?
    Last edited by Kasracer; Dec 31st, 2005 at 06:06 AM.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

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

    Re: CountDown Timer

    All you need is to add a Timer object to your form with an Interval of 1000 (1 second). You can then use the following code to effect your count-down.
    VB Code:
    1. Private endTime As Date
    2.  
    3.     Private Sub StartTimer()
    4.         Me.Label1.Text = "2:00:00"
    5.         Me.endTime = Date.Now.AddHours(2)
    6.         Me.Timer1.Start()
    7.     End Sub
    8.  
    9.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    10.         Dim remainingTime As TimeSpan = Me.endTime.Subtract(Date.Now)
    11.  
    12.         Me.Label1.Text = String.Format("{0}:{1:d2}:{2:d2}", remainingTime.Hours, remainingTime.Minutes, remainingTime.Seconds)
    13.  
    14.         If Me.endTime >= Date.Now Then
    15.             Me.Timer1.Stop()
    16.             MessageBox.Show("Time's up.")
    17.         End If
    18.     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

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