The COM Course - Part 1, Page 7
Events are relatively simple to use.
First off, you have to define an event. This means you tell Visual Basic what the event will be called, plus any parameters it may have. As an example, a Command Button has the Click event, no parameters. On the other hand, the Text Box has the KeyPress event, which also passes something called a 'KeyAscii' value.
So to define an event, you put something like this in the General Declarations section (the top bit) of your class:
Public Event MyEventName(PossArguments As String, Etc As Variant)
And then to 'raise' an event in code, you use the RaiseEvent method in your code. Say you have an OnAdd event that fires off each time you add a new customer to your database. Just before you fire update the database, you might want to 'raise' the event. You could do that like this:
RaiseEvent MyEventName("PossArgs", "Etc")
So let's spend a final few minutes adding and raising our own events. First off, let's define our event:
- Add the following code to the General Declarations section of your class:
Public Event Awake()
Now we need to add the Sleep event. In this event, I could be doing anything adding customer information to a database, cycling through the long process of creating management reports, anything!
But all I am going to do is add a jabberwocking loop that wastes a bit of time, then raises the Awake event. Let's code that, erm, code now:
- Add the following Sub to your CDog class:
Public Sub Sleep()Dim i As LongFor i = 1 To 1000000DoEvents: DoEvents: DoEventsNext iRaiseEvent AwakeEnd Sub
This code simply loops around a million times, doing essentially nothing. On my computer, this loop lasts around a minute or so. Yours may be slower. Or it may be faster. It all depends on gravitational pull. Or processor speed. I forget. But after that short time delay, our Sleep Sub raises the Awake event.
So how can our test application respond to this Awake event? With Command Buttons, it's darn simple. You just enter the code window, select your Command Button from the drop-down list and Bob's Your Uncle.
But in that instance, you're working with a control, something you can see on a Form. Here we're working with pure code, nothing visible.
So if we want to receive events from our code, we need to do something a little special.
- In the General Declarations (top) of your Form code window, declare the following:
Dim WithEvents MyDog As CDog
This code is no different from our previous MyDog declaration, except (a) it has the WithEvents keyword to tell Visual Basic it wants to 'receive' any events, and (b) it is in the General Declarations section, which it must be in order to receive events.
Now let's add a little code to test our new object:
- Delete all code behind the Command Button on Form1
- Insert the following code behind Command1:
Set MyDog = New CDogMyDog.Name = "Billy"MyDog.BarkMyDog.Sleep
This code simply sets the MyDog object to a new instance of your CDog class. It then sets the dog Name, followed by a standard Bark and finishes off by running the Sleep Sub.
Now let's add a little code to respond when your pup has awoken.
- In the code window behind Form1, select 'MyDog' from the 'Object' drop-down list
- In the 'Awake' event of 'MyDog', enter the following code:
Private Sub MyDog_Awake() MsgBox "Your pooch has awoken!"End Sub
Excellent!! Now let's test our application:
- Press F5 to run your application
- Hit your Command Button
After a quick bark, your pup will take a nap, then give you a call when it has finished! Wizard!