Timer countdown?

Hi, Ive looked for like 4 hours and this is only for fun...

How do I make a timer countdown to midnight for example. And could there be answers with the time to countdown to as what would be select in a combo box thanks.

Nothing to hard don't strain yourself trying to help me...



Answer this question

Timer countdown?

  • jwrodriguez

    Hi,

    Or just to countdown in minutes and seconds.>>

    Regards,

    S_DS

    ____________________________________

    Public Class Form1

    Dim mins As Integer = 5

    Dim secs As Integer = 0

    'Set the timer to 1000 milliseconds = 1 second intervals.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Timer1.Interval = 1000

    End Sub

    'Adjust the countdown every second!!

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

    Static minutes As Integer = mins

    Static seconds As Integer = secs

    If minutes < 0 And seconds = 59 Then

    Timer1.Stop()

    MessageBox.Show("Countdown finished!!")

    Exit Sub

    End If

    Me.Text = minutes.ToString & " : " & seconds.ToString

    If seconds = 0 Then seconds = 59 : minutes -= 1 : Exit Sub

    seconds -= 1

    End Sub

    'Start the countdown!!

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Timer1.Start()

    End Sub

    'Stop the countdown!!

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Timer1.Stop()

    End Sub

    End Class



  • MuscleHead

    Oops I put the Timer1.Start in the wrong place thanks...

    It works, now that I fixed it


  • Xigorph

    well basically you set the timer interval! so you could set the interval to 1 second (1000 milliseconds) and this will tick every second.

    in the tick event, simply find the difference between current time and the destination value using a TimeSpan object. you would need to declare your destination value globally so it can take that and compare it with the current time. something like...

    public class Form1

    Dim destinationTime as DateTime = DateTime.Now.AddHours(2) 'example

    private sub Timer1_Tick(byval sender as object, byval e as EventArgs) handles Timer1.Tick

    Dim theDiffTime as TimeSpan = Me.destinationTime - DateTime.Now

    Me.theLabel.Text = theDiffTime.ToString()

    end sub

    end class

    does this help



  • AntonioMaia1

    No, Thats doesn't make the label do anything...


  • Chetan Garude

    well what happens it should show you the time difference

    remember to enable the timer and start the timer on say a button click or on the form load event



  • Gravy

    Or instead of using timespan calculate difference in seconds.

    Something like the following

    Public Class Form1
    Const SecondInHour As Integer = 3600
    Const SecondInMinute As Integer = 60
    Const MidnightTimeInSeconds = 86400 '24* 3600

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Timer1.Interval = 1000
    Me.Timer1.Enabled = True
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    '//Calculate Time to Midnight
    Dim CurrentTime As Integer = (Now.Hour * SecondInHour) + (Now.Minute * SecondInMinute) + Now.Second
    Dim TimeDifferenceInSeconds = MidnightTimeInSeconds - CurrentTime

    '//Split Diffence Into Hours, Minutes and Seconds
    Dim hourstogo As Integer = (TimeDifferenceInSeconds \ SecondInHour)
    Dim minutestogo As Integer = (TimeDifferenceInSeconds - (hourstogo * SecondInHour)) \ SecondInMinute
    Dim secondstogo As Integer = TimeDifferenceInSeconds - ((hourstogo * SecondInHour) + (minutestogo * SecondInMinute))

    '//Display Remaining Time
    Me.Text = hourstogo.ToString & ":" & minutestogo.ToString & ":" & secondstogo.ToString
    End Sub

    End Class


  • Timer countdown?