Title | Make a class that monitors events raised by an object (including a control) |
Keywords | events, monitor events, watch events |
Categories | Software Engineering, Tips and Tricks |
|
|
Create a class that has a Private control variable declared WithEvents. Give it an initialize routine so a form can pass it a reference to the control that it should monitor.
The following code shows the Class1 class. Note the private variable declared using WithEvents. Notice also how the Init routine initializes that variable and how the class catches the Click event.
|
|
Private WithEvents TheButton As CommandButton
Public Sub Init(ByVal cmd As CommandButton)
Set TheButton = cmd
End Sub
Private Sub TheButton_Click()
MsgBox "Here I am"
End Sub
|
|
The following code shows how the main program creates and initializes the Class1 object. Now whenever the user clicks the button Command1, the class catches the event and takes action.
|
|
Private the_object As Class1
Private Sub Form_Load()
Set the_object = New Class1
the_object.Init Command1
End Sub
|
|
|
|