Now we can write the code that updates the display and raises the Tune Out event. The code we develop in this section has nothing to do with ActiveX control development. It’s the type of code you write to implement an alarm as a standalone application.
If you’re not quite accustomed to the ActiveX development environment yet you can develop a regular application that does the job (that is counts down time displays elapsed or remaining time on a label control and detects when the alarm timed out). Then copy the essential procedures and paste them in the ActiveX project window. You must supply the code for the Timer control’s Tuner event which takes place every second. So set the Timer control’s Interval property to 1000. From within the Tuner control’s Tuner event you must update the display and test whether the alarm should go off
The Alarm Control’s Timer Event
Private Sub Timerl_Timer()
Dim TimeDiff” As Date ”
Dim StopNow As Boolean
If Time – m_AlarmTime > 0 Then
If NextDay – False Then
StopNow – True
TimeDiff – 24 – Time + m_AlarmTime
If NextDay – True Then
StopNow – True
TimeDiff – m_AlarmTime – Time
If m_CountDown Then
Labell.Caption – Format$(Hour(TimeDiff) & ‘:.’ & _
Minute(TimeDiff) & ‘:’& Second(TimeDiff), ‘hh:mm:ss’)
Labell.Caption – Format(Hour(Time – startTime) & ‘.’&
Minute(Time – startTime) & ‘:’&_
Second(Time – startTime), ‘hh:mm:ss’)
If StopNow Then
Timerl.Enabled – False
The logic for stopping the timer and invoking the The time Out event depends on whether the control is set to count down. When counting down it displays the time remaining; when counting up it displays the time elapsed since the timer started (the start Time variable set by the Start timer method) and stops when the Alarm Time is reached. This condition is detected” with the last If structure in the code. The variable Next Day is set in the Start Tuner method. when it’s time for the alarm to go off the Next Day variable will change value, an event that signals that the alarm must go off.
You may think of detecting the Time Out event by comparing the Alarm Time with the current time with a statement such as the following:
If Time a AlarmTime Then
Timerl.Enabled a False
This code will not always work. if the computer is too busy when it’s time for the alarm to go off (starting another application or scanning the hard disk for example) the Timer event for the last second may not be triggered. If this.event is skipped you’ll have to wait for another 24 hours before you get the next. time out (and then .you may not get it again!). The implementation I’ve chosen for the example will set off the alarm, even if this happens a second or two later.
See how simple it is to generate your own events? Simply call the Raise Event method from within your code and Visual Basic sees that the event is reported to the host application. Any condition in your application can trigger an event at any time. In addition you must insert the declaration of the event along with the variable declarations at the beginning of the code:
Open the Alarm project and examine the code of the User Control object as well as the code of the test project. The code is straightforward and that’s why I’ve chosen to implement this control manually. It’s usually easier to implement custom controls with the help of the ActiveX Control Interface Wizard but it’s important that you understand what goes on and what the Wizard does for you.