The story for custom control events is much the same as for custom control methods:
You implement an event for your ActiveX control in pretty much the same way that you implement
events for other ActiveX components and for classes in general. Besides the custom events
that you create, you can also implement delegated events or custom events that provide wrappers
for the events of an ActiveX control's constituent controls.
Declaring and Raising Events
You declare an event for a custom control just as you would declare an event in
any Class module:
In the UserControl object's General Declarations section
you put a declaration beginning with the words "Public Event" followed by the event
name you wish to use and then parentheses enclosing a list of the names and types of any parameters
that the event will use. For example, let's say you declared a UserControl
event called FoundOne that passes a single string parameter
describing the object it has found. It would look like the following:
Public Event FoundOne(FoundName As String)
You must then put code somewhere else in the UserControl's
code to actually fire the event with the RaiseEvent
statement:
RaiseEvent FoundOne("Morsel")
Developers who use your control in their applications will see event procedures
in their code windows for the Found event.
Implementing a Default User Interface Event with the Procedure Attributes Dialog Box
Imagine that you've just placed a new form in a standard VB project.
Now imagine that you double-click the form before doing anything else—quick! What do you
see? Of course, anyone who's programmed for more than a few days with VB will answer,
"The Form/Load event procedure."
The Load event procedure is the Form event that
the VB editor will show you by default, all other things being equal (that is if there's
been no code placed in any other event procedures). The Load event is
the default user interface event—the event whose procedure will show up by default in the
Code Window if no other event procedures contain code. You can probably think of the default
user interface events for many other controls: Click for a CommandButton,
Change for a TextBox, and so on.
You can give your custom control its own default user interface event by following
these steps:
STEP BY STEP
13.1 Giving Your Custom Control Its Own Default User Interface Event
-
Make sure you're in a Code Window for the UserControl object.
-
Bring up the Tools, Procedure Attributes option on the VB menu.
-
In the resulting dialog box Name field, choose the name of the custom event
you wish to designate as the default user interface event.
-
Click the Advanced button to bring up more choices.
-
In the Attributes section, check the box labeled User Interface Default.
-
Click OK to save your choice and exit the dialog box.