You can also declare an ActiveX (non-intrinsic) control using the
WithEvents keyword and then write program code to react to its events.
However, the techniques that you must use to program with an ActiveX control and
its events are quite different from those used for an intrinsic control.
First, you must declare the control's object variable as of type
VBObjectExtender. Use the WithEvents keyword
in the declaration if you wish to program with the control's events.
In order to program the control's event procedures, you will have to use a
general event handling procedure belonging to the VBObjectExtender
control object known as the ObjectEvent procedure.
You can find this procedure among the object's
event procedures by navigating to it in the
Code window, just as you would navigate to an
intrinsic control object's event procedures
(see the previous section).
The ObjectEvent procedure takes a single parameter
known as Info. The Info parameter's type is EventInfo.
The EventInfo type contains two properties:
-
Name This property gives
the name of the Event that has fired.
-
EventParameters The fact that its name is a plural word should
tip you off that the EventParameters property is a
collection of EventParameter objects. Like all VB collection
objects, EventParameters has a Count property. If Info.EventParameters.Count
is greater than 0, then the event that's just fired has parameters.
Each EventParameter object has a Name property and a
Value property. If you know the name of a parameter that interests you,
then you can use that name in a string variable to index the Info object's
EventParameters collection and so read or write the value of the parameter
in question (use the Value property of the
EventParameter object).
The example in Listing 4.10 shows code that will tell you the name of any
event that fires for the control that the event procedure belongs to. If the event
supports one or more parameters (the code checks the Count
property of the EventParameters collection),
then the code will show the names and values of those parameters by looping through
the EventParameters collection.
________________________________
LISTING 4.10
ObjectEvent PROCEDURE CODE THAT WILL DISPLAY THE NAME OF ANY EVENT THAT FIRES,
AS WELL AS THE NAMES AND VALUES OF ANY PARAMETERS THAT THE EVENT SUPPORTS
Private Sub mscCalendar_ObjectEvent(Info As
EventInfo)
'Display the name of the event
'that has just fired
Me.Print Info.Name
'If the event has any parameters
If Info.EventParameters.Count > 0 Then
'set up a variable to hold each
'object visited in the For...Each loop
Dim evinf As EventParameter
'Loop through the EventParameters collection
For Each evinf In Info.EventParameters
'For each Parameter object found
'Display its name and value
Me.Print , evinf.Name, evinf.Value
Next evinf
End If
End Sub
The code in Listing 4.11 was written for an MSCalendar
control's ObjectEvent procedure and will prevent
the user from setting the date to the first of the month. The code in the
Calendar control's ObjectEvent procedure checks
to see if the BeforeUpdate event has fired. If the
event at hand is the BeforeUpdate event, then the code
checks to see if the user is trying to set the day of the month to the first.
If the user is indeed trying to set the day of the month to the first, then the
code uses the Info object's EventParameters Collection
to set the Cancel parameter's value to True. Setting the Cancel parameter to True
will cancel the user's action.
LISTING 4.11
ObjectEvent PROCEDURE CODE FOR AN MS CALENDAR CONTROL THAT WILL PREVENT THE USER
FROM SETTING THE DAY OF THE MONTH TO THE FIRST
Private Sub mscCalendar_ObjectEvent(Info As EventInfo)
'Get the name of the event that has fired
Dim strEventName As String
strEventName = UCase$(Trim$(Info.Name))
'If it's the BeforeUpdate event
If strEventName = "BEFOREUPDATE" Then
'If the user is trying to set the
'day-of-month to be the first, then
If mscCalendar.object.Day = 1 Then
'Set the Cancel parameter to True
'to undo the user's change
Info.EventParameters("Cancel").Value = True
End If
End If
End Sub
Note in Listing 4.11 that any reference to the control's custom
members (refer to the Day property in the example)
must be made through the Object property of the VBObjectExtender
variable.
If the code referred to standard members (such as Top, Left, or Visible), it
would not use the Object property. In fact you would
receive a runtime error if you attempted to refer to a standard property through
the Object property.
Adding and Deleting Controls Dynamically Using the Controls Collection
Topics
Related Topics
See Also
<< Previous | Contents
| Next >>