Timer: Elapsed event doesn't trigger
I've started learning vb.net and created a custom timer class. The problem is that the elapsed event doesn't trigger. Can anybody tell me why this is the case?
Option Strict On
Public Class Form1
Dim WithEvents MyTimer As CustomTimer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyTimer = New CustomTimer()
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
MyTimer.HourLeft = Convert.ToInt32(TimePicker.Value.Hour)
MyTimer.MinuteLeft = Convert.ToInt32(TimePicker.Value.Minute)
MyTimer.SecondLeft = Convert.ToInt32(TimePicker.Value.Second)
Call MyTimer.SetTimerLabel()
MyTimer.Interval = 1000
MyTimer.Start()
End Sub
Private Sub MyTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles MyTimer.Elapsed
Call MyTimer.TimerCountdown()
End Sub
End Class
Option Strict On
Public Class CustomTimer
Inherits System.Timers.Timer
'timer properties
Private m_HourLeft As Integer
Private m_MinuteLeft As Integer
Private m_SecondLeft As Integer
Public Sub New()
'initialize countdown values and call SetTimerLabel to set the timer to default values
HourLeft = 0
MinuteLeft = 0
SecondLeft = 0
Call SetTimerLabel()
End Sub
Public Property HourLeft() As Integer
Get
Return m_HourLeft
End Get
Set(ByVal value As Integer)
m_HourLeft = value
End Set
End Property
Public Property MinuteLeft() As Integer
Get
Return m_MinuteLeft
End Get
Set(ByVal value As Integer)
m_MinuteLeft = value
End Set
End Property
Public Property SecondLeft() As Integer
Get
Return m_SecondLeft
End Get
Set(ByVal value As Integer)
m_SecondLeft = value
End Set
End Property
Public Sub SetTimerLabel()
'Create displayvariables (as strings)
'and add zeros if necessary for correct (double digit) display
Dim HourLeftString As String = m_HourLeft.ToString
Dim MinuteLeftString As String = m_MinuteLeft.ToString
Dim SecondLeftString As String = m_SecondLeft.ToString
If HourLeftString.Length = 1 Then HourLeftString = "0" & HourLeftString
If MinuteLeftString.Length = 1 Then MinuteLeftString = "0" & MinuteLeftString
If SecondLeftString.Length = 1 Then SecondLeftString = "0" & SecondLeftString
'Set timer countdown window
Form1.lblCountdown.Text = HourLeftString & " : " & MinuteLeftString & " : " & SecondLeftString
End Sub
Public Sub TimerCountdown()
'for testing purposes, still incomplete...
m_SecondLeft = m_SecondLeft - 1
Call SetTimerLabel()
End Sub
End Class
Create a lblCountdown on the form and a btnStart. Also add a timepicker control with the format property set to time. To see the problem, execute the program, enter some digit in the second box and hit btnStart. The value will not countdown like specified in the TimerCountdown method.

