Adding an Event to a Form

See Also

The following step by step procedure shows how you can create custom events for forms. To try this exercise, open a new Standard Exe project and do the following:

To add an event to Form1

  1. On the Project menu, select Add Class Module to add a class module to the project. Place the following code in the Declarations section of Class1:
    Public Property Get Form1() As Form1
       Set Form1 = mForm1
    End Property
    
    Public Property Set Form1(ByVal NewForm1 As Form1)
       Set mForm1 = NewForm1
    End Property
    

    If you're using Procedure View, the property procedures can't be viewed at the same time. Click the Full Module View button at the bottom left corner of the code window to switch to Full Module View. You can return to Procedure View by clicking the Procedure View button next to it. (Hover the mouse over the buttons to see which is which.)

  2. Add the following code to the Declarations section of Form1:
    Event Gong
    Private mc1 As Class1
    

    Now that Class1 has been created, it's possible to create a variable of type Class1. This procedure switches between Form1 and Class1 several times, because a step in one module requires first adding code to the other.

  3. Go back to Class1 and add the following code to the Declarations section.
    Private WithEvents mForm1 As Form1
    

    As discussed in "Adding Events to a Class," the WithEvents keyword means this instance of Form1 is associated with events. Note that this step wasn't possible until the Gong event had been created.

  4. In the left-hand (Object) drop down on Class1's Code window, select mForm1 to get the event procedure for the Gong event. Add the following code to the event procedure:
    Private Sub mForm1_Gong()
       MsgBox "Gong!"
    End Sub
    
  5. Go back to Form1. In the Object drop down, select Form. In the right-hand (Procedure) drop down, select Load. Add the following code to the event procedure:
    Private Sub Form_Load()
       Set mc1 = New Class1
       Set mc1.Form1 = Me
    End Sub
    

    The first line creates a Class1 object, and the second assigns to its Form1 property (created in step 1) a reference to Form1 (that is, Me — when you're in Form1's Code window, Me refers to Form1; when you're in Class1's Code window, Me refers to Class1).

  6. Put three text boxes on Form1. Use the Object and Procedure drop downs to select the Change event procedure for each control in turn, and place the same line of code in each:
    Private Sub Text1_Change()
       RaiseEvent Gong
    End Sub
    

    Each time the contents of a text box change, the form's Gong event will be raised.

  7. Press F5 to run the project. Each time you type a character in one of the text boxes, the message box rings a bell. It's very annoying, but it shows how you can add an event to a form, and thus get notifications from several controls.

As shown in "Declaring and Raising Events," you can add arguments to events. For example, you might pass the name of the control — or better still, a reference to the control — to the receiver of the event.