Tutorial 1, Part II - Create the Callback class

Create a custom Callback class to use in conjunction with the cControlEventHandlerCommandButton class created in Part I.

Contents

Step 1 - Create a new Callback class.

Step 2 - Add the callback functions.

Step 3 - Add the Form-level code.


Step 1 - Create a new Callback class

1. Insert a new class module in the VBA project.
2. Rename the class to cMyCallbacks.
3. Open the cCallback_TEMPLATE class module, select all, and copy to the clipboard.
4. Open the cMyCallbacks module, select all, and paste.
5. Save the changes.

Top


Step 2 - Add the callback functions

1. In the cMyCallbacks class, replace all occurences of the word "MyFullSignatureCallback" with "Callback1" (use CTRL+H).
2. Replace all occurences of the word "MyPartialSignatureCallback" with "Callback2".
3. In the two callback procedures, add the following code:

Private Sub Callback1(handler As iControl_Base, eventname As String, val1 As Variant, val2 As Variant, val3 As Variant, val4 As Variant)

    MsgBox "Callback1" & vbCrLf & _
        "Handler: " & handler.Name & vbCrLf & _
        "Event: " & eventname

End Sub

Private Sub Callback2()

    MsgBox "Callback2"

End Sub

4. Save changes.

Top


Step 3 - Add the Form-level code

1. At this point we need to declare two new variables. One will hold a reference to the cControl_EventHandler_CommandButton object we create, the other creates a new instance of the cMyCallbacks class. Add the following lines to the start of the forms Form_Open() procedure:

Dim cmdHandler As cControl_EventHandler_CommandButton
Dim callbacks As iCallback

2. Create a new instance of the cMyCallbacks class by adding the line immediately after the variable declarations.

Set callbacks = New cMyCallbacks

3. Modify the one existing line of code to read:

Set cmdHandler = CreateEventHandler(CreateGenericReference(Me.Command0, ccCommandButton))

4. Add the following lines of code to attach the callback to the command button event handler's OnClick event:

With cmdHandler.OnClick
   .AddCallback callbacks, "Callback2"
   .AddCallback callbacks, "Callback1", ccFullSignature
End With


5. Save the project and run the AutoExec macro. Click the test button. You should get a message box which reads:

tut1-1.JPG

which indicates that the callback was successfully attached to the test command button's OnClick event.
Another message box will pop up indicated Callback2 was also executed. Note that the callbacks are executed in the reverse of the order in which they are added!

The form-level code should read as follows:

    Dim cmdHandler As cControl_EventHandler_CommandButton
    Dim callbacks As iCallback
    
    Set cmdHandler = CreateEventHandler(CreateGenericReference(Me.Command0, ccCommandButton))

    Set callbacks = New cMyCallbacks
    
    With cmdHandler.OnClick
        .AddCallback callbacks, "Callback2"
        .AddCallback callbacks, "Callback1", ccFullSignature
    End With


A few last notes:

  1. Every control event handler has a Startup and Terminate event. You can assign callbacks directly to these events, if you so choose. Typically, these events are tied to the Open / Close event of the control's parent form.
  2. You can still trap the ActiveX control events at the Form level, if you wish. These traps occur outside the Composite Control object model and will occur before any event trapping occurs within the object model.

Top

Last edited Mar 3, 2011 at 11:08 AM by vba_junkie, version 7

Comments

No comments yet.