Share via


Visual Basic Concepts

Receiving an Asynchronous Notification Event

When you use events for asynchronous notifications, most of the work is done by the component author. Setting up a client to receive the events is pretty easy.

Note   This topic is part of a series that walks you through creating a sample ActiveX EXE. It begins with the topic Creating an ActiveX EXE Component.

To receive the CoffeeReady event in CoffeeWatch

  1. Switch to the instance of Visual Basic that has the CoffeeWatch project loaded.

  2. In the Project Explorer window of the CoffeeWatch project, right-click Form1 to open the context menu, and select View Code to open the code window. Comment out the code in the Click events of the two buttons.

  3. Modify the code in the Declarations section as follows:

    Option Explicit
    Private WithEvents mwcmnTest As CoffeeMonitor
    

    Notice the letter ‘w’ added to the variable mwcmnTest, to remind the author that this is a WithEvents variable. This is one author’s private convention. The letters ‘cmn’ have been chosen to indicate a variable of type CoffeeMonitor.

  4. Add the following code to the Load event of Form1, to create a new CoffeeMonitor object and assign it to the WithEvents variable. At run time, Visual Basic connects the object’s events to their event procedures when this assignment takes place.

    Private Sub Form_Load()
       Set mwcmnTest = New CoffeeMonitor
    End Sub
    
  5. In the Object drop down, select mwcmnTest to obtain access to its event procedures. Add the following code to the CoffeeReady event.

    Private Sub mwcmnTest_CoffeeReady()
       MsgBox "COFFEE!"
    End Sub
    
  6. That’s it. Press F5 to run the project.

    Every ten seconds you’ll get a notification from Coffee. CoffeeWatch is not blocked in the meantime — you can move and resize it, and click the buttons.

  7. Close the CoffeeWatch form to return to design mode.

Step by Step

This topic is part of a series that walks you through creating a sample ActiveX EXE.

To See
Go to the next step Sharing the CoffeeMonitor
Start from the beginning Creating an ActiveX EXE Component