I am new to vb and trying to create a timer that will countdown for sets of time interval. I managed to display the countdown for enddate but I have no idea how to continue to enddate1 as soon as enddate finished.

My code as follows:

Public Class Form1

Dim startdate As DateTime = DateTime.Parse(Date.Now)
Dim enddate As DateTime = DateTime.Parse("14:41")
Dim enddate1 As DateTime = DateTime.Parse("14:42")

Dim ts As TimeSpan = enddate.Subtract(startdate)

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

Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ts = ts.Subtract(New TimeSpan(0, 0, 1)) '~~~ Subtract 1 second
'~~~ Display it
Label1.Text = CStr(ts.Hours) & " Hours: " & CStr(ts.Minutes) & " minuter: " & CStr(ts.Seconds) & " second"

'~~~ Check if the countdown is finished or not
If Math.Floor(ts.TotalSeconds) = 0 Then
Timer1.Stop()
MessageBox.Show("Finished !")
End If
End Sub

End Class

You could try something like this:
change line 5

dim Flag as boolean
Dim ts,ts1 As TimeSpan

change line 10

ts = enddate.Subtract(startdate)
ts1 = enddate1.Subtract(startdate)    
Flag = True

change line 21

Timer1.Stop()
If Flag = True Then
    ts=ts1
    Flag = False
    Timer1.Start()
EndIf

it works, but how about if i want to put more enddate into it?
What should i change in the timer coding?

Public Class Form1

Dim startdate As DateTime = DateTime.Parse(Date.Now)
Dim enddate As DateTime = DateTime.Parse("06:00")
Dim enddate1 As DateTime = DateTime.Parse("06:30")
Dim enddate2 As DateTime = DateTime.Parse("07:00")
Dim enddate3 As DateTime = DateTime.Parse("07:35")
Dim enddate4 As DateTime = DateTime.Parse("08:00")
Dim enddate5 As DateTime = DateTime.Parse("08:30")
Dim enddate6 As DateTime = DateTime.Parse("09:00")
Dim enddate7 As DateTime = DateTime.Parse("10:00")
Dim enddate8 As DateTime = DateTime.Parse("11:00")
Dim enddate9 As DateTime = DateTime.Parse("12:00")
Dim enddate10 As DateTime = DateTime.Parse("13:00")
Dim enddate11 As DateTime = DateTime.Parse("14:00")
Dim enddate12 As DateTime = DateTime.Parse("15:00")
Dim enddate13 As DateTime = DateTime.Parse("16:00")
Dim enddate14 As DateTime = DateTime.Parse("17:00")
Dim enddate15 As DateTime = DateTime.Parse("17:30")
Dim enddate16 As DateTime = DateTime.Parse("18:00")
Dim enddate17 As DateTime = DateTime.Parse("18:30")
Dim enddate18 As DateTime = DateTime.Parse("19:00")
Dim enddate19 As DateTime = DateTime.Parse("19:30")
Dim enddate20 As DateTime = DateTime.Parse("20:00")
Dim enddate21 As DateTime = DateTime.Parse("21:00")
Dim enddate22 As DateTime = DateTime.Parse("22:00")
Dim enddate23 As DateTime = DateTime.Parse("23:00")
Dim Flag As Boolean
Dim ts, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ts8, ts9, ts10, ts11, ts12 As TimeSpan
Dim ts13, ts14, ts15, ts16, ts17, ts18, ts19, ts20, ts21, ts22, ts23, constant As TimeSpan
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Interval = 1000
    Timer1.Enabled = True
    ts = enddate.Subtract(startdate)
    ts1 = enddate1.Subtract(startdate)
    ts2 = enddate2.Subtract(startdate)
    ts3 = enddate3.Subtract(startdate)
    ts4 = enddate4.Subtract(startdate)
    ts5 = enddate5.Subtract(startdate)
    ts6 = enddate6.Subtract(startdate)
    ts7 = enddate7.Subtract(startdate)
    ts8 = enddate8.Subtract(startdate)
    ts9 = enddate9.Subtract(startdate)
    ts10 = enddate10.Subtract(startdate)
    ts11 = enddate11.Subtract(startdate)
    ts12 = enddate12.Subtract(startdate)
    ts13 = enddate13.Subtract(startdate)
    ts14 = enddate14.Subtract(startdate)
    ts15 = enddate15.Subtract(startdate)
    ts16 = enddate16.Subtract(startdate)
    ts17 = enddate17.Subtract(startdate)
    ts18 = enddate18.Subtract(startdate)
    ts19 = enddate19.Subtract(startdate)
    ts20 = enddate20.Subtract(startdate)
    ts21 = enddate21.Subtract(startdate)
    ts22 = enddate22.Subtract(startdate)
    ts23 = enddate23.Subtract(startdate)
    Flag = True
    Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    ts = ts.Subtract(New TimeSpan(0, 0, 1)) '~~~ Subtract 1 second
    '~~~ Display it
    Label1.Text = CStr(ts.Hours) & " Hours: " & CStr(ts.Minutes) & " minuter: " & CStr(ts.Seconds) & " second"
    '~~~ Check if the countdown is finished or not
    If Math.Floor(ts.TotalSeconds) = 0 Then
        Timer1.Stop()
        If Flag = True Then
            ts = ts1
            Flag = False
            Timer1.Start()
        End If
        MessageBox.Show("Finished !")
    End If
End Sub

End Class

You won't need the flag anymore. Make an array enddate(24). Since your values are inconsistent you'll have to initalize the enddate array the same as your doing now. something like:

dim enddate(24) as DateTime
enddate(0)= DateTime.Parse("06:00")
enddate(1)= DateTime.Parse("06:30")
etc

Have only 1 ts, dim ts as TimeSpan. In a for loop add your timespans together, ts += enddate(i).Subtract(startdate). This will run all your timespans end to end.
Sorry I don't have access to VS right now and I'm doing this off the top of my head, let me know if I've forgotten something.

I cannot proceed to the next enddate array. it stops after enddate(0). I tried to put for loop in form1 sub and also timer sub. But still doesn't work. Any idea?

Dim startdate As DateTime = DateTime.Parse(Date.Now)
Dim i As New Integer
Dim enddate(4) As DateTime
Dim ts As TimeSpan
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Interval = 1000
    Timer1.Enabled = True
    i = 0
    enddate(0) = DateTime.Parse("02:07:00")
    enddate(1) = DateTime.Parse("02:08:00")
    enddate(2) = DateTime.Parse("02:09:00")
    enddate(3) = DateTime.Parse("02:10:00")
    enddate(4) = DateTime.Parse("02:11:00")
    ts += enddate(i).Subtract(startdate)
    For i = 0 To 4

    Next
    Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    ts = ts.Subtract(New TimeSpan(0, 0, 1)) '~~~ Subtract 1 second
    '~~~ Display it
    Label1.Text = CStr(ts.Hours) & " Hours: " & CStr(ts.Minutes) & " minuter: " & CStr(ts.Seconds) & " second"
    '~~~ Check if the countdown is finished or not
    If Math.Floor(ts.TotalSeconds) = 0 Then
        Timer1.Stop()
        MessageBox.Show("Finished !")
    End If
End Sub

try this:

    For i = 0 To 4
            ts += enddate(i).Subtract(startdate)
    Next

tried it by replacing from line 14 to 17 but not what i am looking for. it adds up the total countdown time. not executing it one by one

Ok that's a little more involved. First add a counter intialized at 0. Then make ts an array of 24 timespans. in the for loop use ts(i)=enddate(i).subtract(startdate). add tstemp as timespan = ts(0). In Timer1_Tick change ts to tstemp, change lines 25-28:

    If Math.Floor(ts.TotalSeconds) = 0 Then
        counter += 1
        if counter <> ts.length then
            tstemp=ts(counter)
        else
            Timer1.Stop()
            MessageBox.Show("Finished !")
        End If
    End If

this will reset the timer countdown to the next timespan at the end of each countdown.

I tried, but the the timer freezes

Whewn I tried to move Dim tstemp As TimeSpan = ts(0) to line 5, it starts to count backward (-1,-2,-3....). Move it to line 21 the timer frezes.

Got it figured out here's the code:

Public Class Form1


    Dim startdate As DateTime = DateTime.Parse(Date.Now)
    Dim enddate(5) As DateTime
    Dim tstemp As TimeSpan
    Dim ts(5) As TimeSpan
    Dim Counter As Integer = 0


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

        Timer1.Interval = 1000
        Timer1.Enabled = True
        enddate(0) = DateTime.Parse("23:30:00")
        enddate(1) = DateTime.Parse("23:31:00")
        enddate(2) = DateTime.Parse("23:32:00")
        enddate(3) = DateTime.Parse("23:33:00")
        enddate(4) = DateTime.Parse("23:34:00")
        For i = 0 To 4
            ts(i) = TimeSpan.FromTicks(Math.Abs(enddate(i).Subtract(startdate).Ticks))
        Next

        tstemp = ts(0)
        Timer1.Start()
    End Sub


    Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        tstemp = tstemp.Subtract(New TimeSpan(0, 0, 1)) '~~~ Subtract 1 second
        '~~~ Display it
        Label1.Text = CStr(tstemp.Hours) & " Hours: " & CStr(tstemp.Minutes) & " minuter: " & CStr(tstemp.Seconds) & " second"
        '~~~ Check if the countdown is finished or not
        If Math.Floor(tstemp.TotalSeconds) = 0 Then
            Counter += 1
            If Counter <> ts.Length Then
                tstemp = ts(Counter)
            Else
                Timer1.Stop()
                MessageBox.Show("Finished !")
            End If
        End If
    End Sub
End Class

I think this does what you want now. This counts down each timespan based on each enddate. The reason it would count up was because the timespan was negative. I added some code in the for loop to make sure it's always positive.

yes it works but the countdown is not accurate. anyway i fugured out an alternative to it. What do you think?

Private alarmTime As Date
Private count As Integer = 0

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

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If alarmTime < Date.Now Then
Me.Timer1.Stop()
MessageBox.Show("Time's up.")
If (Me.count < 5) Then 'change 5 to how many times you want the timer to run
SetALarmTime()
End If
Else
Dim remainingTime As TimeSpan = Me.alarmTime.Subtract(Date.Now)

Me.Label1.Text = String.Format("{0} Hours:{1:d2} Minutes:{2:d2} Seconds", _
remainingTime.Hours, _
remainingTime.Minutes, _
remainingTime.Seconds)
End If
End Sub

Private Sub SetALarmTime()
Me.alarmTime = Date.Now.AddSeconds(2) 'Use this to determine your timespan use addseconds or addminutes methods
Me.Timer1.Start()
Me.count = Me.count + 1
End Sub

As long as it works. However it seems to be just as accurate. If you want more accuracy add milliseconds to your display.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.