The Alarm Control’s Interface

The Alarm control has two custom properties:

• Count Down
• Alarm time

Alarm time is the time when the alarm goes off expressed in  M/PM format. Count Down is a True/False property that determines what’s displayed on the control. If Count Down is True the alarm displays the time remaining. If you set the alarm to go off at 8:00 PM and you start the timer at 7:46 PM, the control displays 0:14.00 then 0:13.59 and so on until the alarm goes off 14 minutes later. If Count Down is False the control starts counting at 00:00.00 and counts until the
Alarm Tune is reached. If the Alarm Time property is ahead of the current time  the Alarm control assumes that you want the alarm to go off at the specified time on the next day. For example if the time is 8:00.00 PM on April 1 and the Alarm- .Tune is 6:30.00 :PM the alarm will go off at 6:30.00 PM on April 2 (which is 22 hours and 30 minutes later).

The Alarm control has two methods for starting and stopping the alarm:

Start Timer starts the alarm .
Stop time stops the alarm

Finally the AIarm control has a Time Out event which notifies the application that the alram has gone off (which happens we the time has reached Alarm Tune). The application can use this event to trigger another action or simply to notify the user.

Testing the Alarm Control

The Alarm control’s test Form is shown in Figure 16.16, earlier in this chapter. It contains two instances of the control, and you can change their Count Down property at run time. the Alarm Tune property of the controls is set from within the test Form’s Load event to a random value with the following statements:

Private-Sub Form Load()
.Randomize Time
AlanmCtll.AlarmTime – Rnde)
AlanmCt12.AlarmTime – Rnde)
End Sub

If you run the Alarm test project comment out thees in the Form’s Load event and set the alarm time a few minutes ahead of the current time so that you won’t have to wait long before the alarm goes off.

When the two Check Box controls are clicked, the Count down property is changed with the following code

Private Sub Check_Click()
If Checkl.Value Then
AlarmCtll.CountOown – True
Else
AlanmCtll.CountDown – False
End If
End Sub

The code for the Check2 control’s Click event is identical only it applies to the Alarm Ct12 control. Finally when an Alarm control times out the following code is executed

Private Sub Alarm Ctl1_TimeOut()
MsgBox ‘The Alarm’ &AlanmCtl1.Tag & ‘has Timed out’
End Sub

You must set the control’s Tag property to a meaningful name since the program uses this property to differentiate between the two controls. The test project uses the tags Process A and Process B.

The Start Tuner button under each Alarm control starts the timer of the corresponding control. At the same time it changes its Caption to Stop timer so that the user can stop the countdown at any time. The code behind the first Start Tuner button is shown next.

Starting and stopping the Alarm

Private Sub StartButtonl_Click()
If StartButtonl.Caption – ‘Start Timer’ Then
AlarmCtl 1.StartTimer
StartButtonl.Caption – ‘Stop Timer”
Else
StartButton1.Caption – ‘Start Timer’
Alarmctll.StopTimer
End If
End Sub

Finally the Alarm Tune buttons display the time when the corresponding alarm will go off with the following code:

Private Sub AlarmButton1-Click()
MsgBox ‘The alarm will go off at •& AlarmCtll.AlarmTime
End Sub

Load the Alarm project (continue loading even after the warning that will appear) and replace the two Picture Box controls that will appear in place of the Alarm controls  with two instances of the Alarm control Experiment with the interface of the  control and examine the Code of the test project.

Open the test project, comment out the statements in the Form’s Load event (which set the Alarm Tune properties to random values) and set the Alarm Tune property a minute or so ahead of your system’s time (the current time displayed in the lower right comer of the task bar). Run the project. and then click the Count Down box. The Alarm will start counting downward the difference between the current time and the alarm time. When this difference reaches zero a message box informs you that time’s up. If the Count Down box is cleared you’ll see how much time has passed since you started the timer. To see when the alarm will go off click the Alarm Tune button.

Designing the Alarm Control

Your first step is to design the control’s interface. Unlike the Timer control of Visual Basic the Alarm control has a visible interface. Its operation is based on two constituent controls:

Timer which updates its display every second
Label which displays the time

Designing the User Interface

To design the control’s interface follow these steps:

1. Place a Label control on the User Control Form and set its Font property to a font and size that looks nice for our purposes.
2.Align the Label with the upper left corner of the control and re size the control so that it just fits the label. (Make a note of the values of the control’s Width and Height properties. You’ll need them later when you write the code to.prevent this control from being re sized.)
3. Place a Timer control on the UserControl object. It doesn’t make any difference where the Tuner control is placed; it will be invisible at run time. You can place the timer outside the visible area of the user control or even on top of the label.
4. To complete the design of the control and prevent it from being re sized add the following code to the control’s Re size event: Private Sub User Control_Re size() User Control.Size 1800 500
End Sub

The Size method forces the control to remain at a fixed size. You’must change the values 1800 (width) and SOO(height) to the size of the control, according to the size of the Label control.

Now you can test the behavior of your new control. Place it on a Form and try to re size it. Even though you can drag the handles of the control you won’t be able to resize it

Scroll to Top