Implementing the Controls Members

Now we are ready to implement the control’s properties, its methods, and its event. Let’s start with the properties. You have seen how to add properties to a control with the ActiveX Control Interface Wizard and how to do it manually. Now let’s look at one more tool. We will 40 something similar with the Wizard but this time
one property or method at a time

Let’s start with the variable declarations. Insert the following lines in the User Control object’s Code window

Private start Time As Date
Private Running As Boolean
Private m_CountDown As’ Boolean
Private m_Alarm Time As Date

As you have guessed m_CountDown and mAlarm Time are the two private variables that will hold the values of the CountDown and Alarm Time properties. The Running variable is True while the alarm is running and is declared outside any procedure so that all procedures can access its value. The start Time variable is set to the time the alarm starts counting and is used when the control is not counting down (you’ll see how it’s used shortly).

To add the skeletons for the control’s properties with the Add Procedure command follow these steps:
1. Switch to the User Control window and double-click its Form to open the Code window.
2. Choose Tools > Add Procedure to open the Add Procedure dialog box

3. Add the name of the Count Down property and check the Property radio button. The following lines are inserted in the code window:
Public Property Get Count Down () As Variant
End Property
Public Property Let Count Down (By Val v New Value As Variant)
End Property

4. Change the property’s type to match its declaration, and then supply the code for the two Property procedures:
Public Property Get Count Down() As Boolean
Count Down – m Count Down
End Property
Public Property Let CountDown(ByVal vNewValue As Boolean)
m_CountDown – vNewValue .
End Property

NOTE

the code should be quite trivial by now. All Property procedures map the property name to a private variable and they have the same structure. You must not forget to change their types from Variant to whatever type best describes the variables.

S. Do the same for the Alarm Time variable. The procedures’ for this property are as follows:
Public Property Get Alarm Time() As Date
AlarmTime – m-AlarmTime
End Property
Public Property Let AlarmTime(ByVal vNewValue As Date)
If IsDate(vNewValue) Then m-AlarmTime – vNewValue
End Property
This Property procedure validates the property value to make sure you enter a valid time. If you specify a date the program assumes that the time is 00:00:00 ‘
(midnight).

6. Now you’re ready to add the two methods. In the Code window choose Tools> Add Procedure. Enter the name of the method Start Timer but this time check the Sub radio button. The following tines will be inserted in the code:
Public Sub Start Timer(
End Sub
7. In the Start Tuner O subroutine insert the code to start the alarm:
Public Sub Start Timer()
If Not Running Then
Timerl.Enabled ~ True
Running – True
startTime – Time

If Time alarmTime > 0 Then NextDay – True
labell.Caption – ’00:00:00′
End If
End Sub

This subroutine doesn’t do anything if the alarm is already running. If it isn’t it enables the Timer control and sets the variable start Timer to the current time and the display to “00:00:00”. The Running variable is also set to True to prevent this subroutine from being executed again while the alarm is running: The variable Next Day is set to True if the alarm time is behind the current time. This means that the control “should go off at the specified time on the next day.

8. Now choose Tools> Add Procedure again to create another public subroutine the Stop1imer() method. Here is the code for this subroutine:
Public Sub StopTimer()
If RunriingThen
Timerl.Enabled – False
Running – False
End If
End Sub
As with the Start timer method the alarm stops only if it’s running. If that’s the case the code disables the The control and sets the Running variable to False.
9. Now add the control’s event Chose Tools> Add Procedure to open the Add Procedure dialog box.

10. Specify the name Time Out “and check the Event radio button. This time a single line is added to the code right after the declarations:
Event TimeOut()
How do you cause this event to take place? With the Raise Event statement from ” ‘any place in your~. Whenever the alarm goes off, you can raise the TimeOut
event with the following statement:
Raise Event Time Out
the Tune Out event is raised &om within the Timer’s code, which is the core of  the control (we’ll see this code shortly). You just completed the seletion of the Alarm control the kind of thing we did earlier with the ActiveX Control Interface Wizard. This time you’ve added all the  members of the control’. interface manually with the help of the Tools menu. It’s easier to let the Wizard build the control for you but knowing how it can be done manually will help you add a property or two to an existing control without having to go through all the steps of the ActiveX Control Interface Wizard.

TIP

If you run the ActiveX Control Interface Wizard to update an existing control you will find that the Wizard comments out some of your code. Always check your code after processing it with the Wizard.

The Add Procedure command will not insert the appropriate lines in the Read- Properties and Write Properties events. You must insert the following code manually.

Saving and Reading the Alarm Control’s Properties

Private Sub User Control_Read Properties(Prop Bag As Property Bag)
CountDown – PropBag.ReadProperty(‘countdown’ CountDown).
m_AlarmTime = PropBag.ReadProperty(“AlarmTime’ AlarmTime)
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty ‘CountDown’ m_CountDown False
PropBag.WrjteProperty ‘AlarmTime’ m_AlarmTime 0
End Sub

Finally you must insert the following code also manually to initialize the values of the properties:
Private Sub UserControl_InitProperties()
m_CountDown = True
Running – False
End Sub

Scroll to Top