Raising Events from within Classes VB Help

“This topic is rather advanced and you can safely skip it. Raising events from with in classes is not a very common practice. Since Classes don’t have a visible  user interface there are no external events to react to and from which to raise events. In the following chapter you ‘team how to raise events from within ActiveX controls have a visible interface and they must react to many external events such as mouse and keyboard events. As you will see it’s
actually quite common to raise events from Within ActiveX controls and quite simple too.

The situation is quite different with ActiveX DLLs. classes raise events based on internal events such as timeouts. We will revise the C timer Class so that it raises a ‘ Minute and an Hour event every time minute or an hour has passed. The revised c timer project along with its test project can be found in the EVN Tuner folder in  this chapter’s folder on the CD.

Since the C timer Class doesn’t keep track of time constantly, we must help it with a Tuner control. But the Class-Module doesn’t have a Form, so how can we use a Tuner control? We’ll add a Form to the project, but we’ll never show it. It will be a hidden Fon with a Timer control. The Class Module will be able to access the Tuner
-control on the Form and intercept its Timer event. Here’s how you can access an ActiveX control from within a Class Module.

1. In the Project Explorer select the C Timer component and rename it to Event Timer as shown in Figure 15.3.Then open the Project menu, select Add Form and add a new Form to the Class Module project.

NOTE

1, In the Project Explorer select the C Timer component and rename it to Event- Timer as shown in Figure 15.3.Then open the Project menu select Add Form and add a new Form to the Class Module project.

2. Open the Form Form in design mode arid place an instance of the Tuner control on it. Set Its’Enabled property to True and its Interval property to 00 סס 1 , This value corresponds to 10 seconds. We don’t want our Time Class to spend too much computer time processing Tuner events, so we set a large timeout interval.

Figure 15.3 shows the Event Tuner project in the Visual Basic IDE. Notice the project components in the Project window, the Form 1 in the Design window and the Class’ Code window. The new Class is called Event Tuner Class and the Project is called Event Tuner. When you open this sample project, remember to select the test project and add a reference to the Event Tuner to it.

the Event Timer Project is.
the same as the C timer
project, but uses an Internal
timer to keep track of time.

FIGURE 15.3:

FIGURE 15.3:

Our goal is to intercept the Tuner control’s Timer event from within the class Module and use it to raise the Minute and Hour events. The Minute event  takes place for the first time 60 seconds after the timer is started and then every 60 seconds thereafter. Likewise the Hour event takes place for the first time 60 seconds after the Class is started and every 60 minutes after that.

3. In order to access a control on another Form, you must create a Form variable in the Class itself. Insert the following declarations in the Class’ code:

Dim c Frm As Form 1
Dim With Events e Timer As Timer

The keyword With Events, tells Visual Basic that the e Timer control must carry with it its properties, methods, and events (which is why I named it e Timer). These variables will come to life when you set them to the appropriate objects. This action must take place from within the Class’ Initialize event.

4. Insert the following lines in the QassJnitia1ize event:
Private Sub Class_Initialize()
Set c Fnm- New Fonml
Load cFnm
Set eTjmer = cFnm.Timerl
.End Sub

The first statement assigns a new instance of the-Form object to the c Frm variable. Then the new Form is loaded. At this point we can access the controls on the Form. The last line makes the e Timer variable equivalent to the  Tuner 1 control on the Form 1 Form. The Class can now access the Timer 1 control on the Form 1 Form as e Timer just as the Form l can’ access it as Timer The two expressions are equivalent, but you can’t access the Timer control as Form l Timer l from within the class Module. If you expend-the Object drop-down list in the Class Module’s Code window  you with ‘the object e timer. Because the e Timer object variable is declared
with the With Events keyword, we should be able to program its events. That’s why its name appears in the Objects list. Select the e Timer object on the Objects list and then open the Events list in the Code window. You will see the name of the Timer event. Select and you’re ready to program the Timer Q event which is equiv~t to programming the Timer _Tuner event.

5. Insert the lines of Code 15.5 in the e Tuner_Tuner event handler.

Raising Events from within a Can

Private Sub e Timer_Timer()
Static seconds As Long
Static minutes As Long
Static hours As Long
Dim Raise Minutes As Boolean. Raise Hours As Boolean

If Not Counting Then Exit.Sub
Raise Minutes – False
Raise Hours – False
seconds seconds’+ e Timer.Interval / 1000′ ,
If second a = 60 Then
minutes – minutes  seconds – 0
Raise nut.s – True
If minutes = 60 Then
hours = hours + 1
minutes = 0
Raise Hours .’True
End if
End If
If Raise Hours Then
Raise Event Hour
ElseIf Rsisemintes Then
End If )’
End Sub

The Counting Boolean variable is declared in the Form’s declaration section and indicates whether the timer is counting. If the timer ~ paused, we shouldn’t process the timer event: This’ variable is ~ to True from with in the Start Counting method  and reset Far from within the Stop Counting method. This sub you tine is invoked every 10 seconds and increases the number of see  and elapsed ~ 10. every 60 seconds, it increases the number of minutes by  of 1, and every 60 minutes it increases the number of hours by in increment. J 1. If the minutes variable is u (that is, if it.has reached 60 and is reset back to 0), ~,J~our event must be raised. If not, we examine the value of the  seconds variable. If of,then the Minute event must be raised-This coding raises 59 consecutive Minute events, then an Hour event, and the process is repeated. Since we can’t issue ‘to events at once, we skip the Minute event when the Hour event is due. Obviously, the Hour event implies that a Minute event should take place.

6. In order for a Class to raise ‘an event, you must also declare the names of the events. Insert the following lines in the Class Module’s code window (outside any procedure):
Event Minute()
Event Hour()

The complete listing of the Event Tuner Class is shown next. Notice its similarities to and differences from the plain C Tuner Class. The Property Get procedures and the Class’ method don’t change. Only the code for accessing a Tuner control on an invisible Form is added, and the Tuner control’s Tuner event is programmed to raise the appropriate events.

The Event Timer Class Listing

Dim c Frm As Form 1
Di.With Events e Timer As Timer
Dim total interval As Double
Dim T 1 As Double
Dim Counting As Boolean
Event Minute()
Event Hour()
Public Sub Start Counting()
T 1 – Time
Counting – “True
End Sub
Public Sub Stop counting O
total interval 1 = total interval ‘+ Time -‘T 1 ‘
Counting ~ False
End Sub
Property Get Elapsed Time() As Double
Elapsed Time – total interval
End Property
Public Sub Reset Timer()
End
Private Sub Class_initialize()
Set c Frm:- New Form 1
Load :c FrIII
Set e Timer – c Frm.Timerl
End Sub ” .
Private Sub e Timer_Timer()
Static seconds As Long
Static minutes As Long
Static hours As Long
Dim Raise Minutes As.Boolean, RaiseHours As Boolean
If Not Counting Then Exit Sub
Raise Minutes = False
Raise Hours = False ‘
seconds = seconds + e Timer.Interval / 1000’
If seconds = 60 Then
minutes = minutes + 1
seconds = 0
Raise Minutes = True
If mintes = 60 Then
hours = hours +’1
minutes = 0
Raise Hours = True
End If
End.If
If Raise Hours Then
Raise Event Hour
ElseIf Raise Minutes Then
Raise Event Minute
End If
End Sub

Testing the Event Timer Class

To test the Event Timer Class, add a new project to the existing one with the File> Add Project command. On the test project’s Form, add a Command button and . four Labels. Arrange the Labels in pairs and align them as shown in Figure 15.4. The controls Labe 13 and Label 4 are where the number. of elapsed minutes and hours are displayed.

The even timer Class
TestFonn

FIGURE 15.4:

FIGURE 15.4:

The Command button lets you start and pause the timer. When you click it for the first time its caption changes to Pause Tuner. To pause the timer click the button again. To resume timing click the button and its caption will Change back to Start Tuner.

When the Form is first loaded an event Timer class object variable is created from within the Form’s toad event with the following statement

Private Sub Form_Load()
Set TMR – New event Timer Class
End Sub

The TMR variable is declared as:
Dim WithEvents TMR As eventTimerClass

As soon as you’ve entered the above declaration in the Form’s declarations section, the TMR object will be added to the Code window’s ‘Objects drop-down list and its events to the Events list. Select the TMR object in the Objects list and exam-line events. in the Events list. Then.select the Minute and Hour events and enter
the following code in their handlers:

Private Sub TMR_Hour()
Labe14.Caption – Va,l(Labe14.Caption) + 1
End Sub
Private Sub Minute()’
Labe13.Caption’· Val(Label 15. caption) + 1
End Sub
Finally add the following code to the Command button’s Click event handler to start and pause the timing:

Private Sub Command1..Cl1ck()
If Commandl. Caption.Caption – ‘Start Timer’ Then
Communication – ‘Pause Timer’
TMR.Start Counting .
Else
command.Caption ‘Start TillM!r’
TMR. Stop Counting
End If
end’Sub

The hours and minutes reported on the Form’s Labels correspond to the time elapsed while the timer is counting.When the timer is paused the Event Tuner  Class doesn’t count seconds or fire any events.

Posted on November 5, 2015 in Building AdiveX Components

Share the Story

Back to Top