Visual Basic Concepts
When you create a user control, it's common to create public events. When the user control is placed on a standard Visual Basic form, the custom events automatically appear in the form's code module.
With the DataRepeater control, however, exposing the user control's events is not an automatic occurrence. Because the user control is not put directly on a form, its events cannot be exposed in the code module. There is, however, a way of achieving the same end.
In brief, the major steps are:
Note The following step-by-step procedure builds on the user control and DataRepeater control project built in the topic "Using the DataRepeater Control."
Creating events to be exposed by the user control
Option Explicit
Public Event ProductChange()
Public Sub FireControlChange()
RaiseEvent ProductChange
End Sub
The ProductChange event is the event that will be exposed by CtlEvents when using the ProductsCtl user control in a DataRepeater control.
Option Explicit
Dim EventsObj As New CtlEvents
Public Property Get Events() As CtlEvents
Set Events = EventsObj ' returns the CtlEvents object reference
End Property
Private Sub txtProductName_Change()
PropertyChanged ("ProductName")
EventsObj.FireControlChange
End Sub
To expose the events of the User Control in the container form
The following steps take place in the project that contains the DataRepeater control. In that project, a DataRepeater control contains the user control with the events created in steps 1 to 9 above.
Option Explicit
Dim WithEvents objRepCtl As CtlEvents ' Be sure to use WithEvents
Private Sub DataRepeater1_RepeatedControlLoaded()
Set objRepCtl = DataRepeater1.RepeatedControl.Events
End Sub
Because you can set the RepeatedControlName property at run time, the RepeatedControlLoaded was designed to allow you to set control properties that can only be initialized after the control has been loaded.
Private Sub objRepCtl_ProductChange()
Debug.Print "objRepCtl_ProductChange"
End Sub
With a few simple additions of code, you can easily add more events to the user control. First add public events — one for each event you want to raise — to the class module:
Option Explicit
Public Event ProductChange()
Public Event UnitPriceChange() ' <- This is a new Event declaration.
Then add an argument to the class module's FireControlChange procedure. A Select Case statement is added to the procedure to distinguish which control is calling the procedure. Using that argument, the correct event is raised:
Public Sub FireControlChange(ctlName As String)
Select Case ctlName
Case "ProductName"
RaiseEvent ProductChange
Case "UnitPrice"
RaiseEvent UnitPriceChange
Case Else
' Handle other cases here.
End Select
End Sub
Finally, switch to the UserControl object's code module. In the Change event of the controls you wish to monitor, invoke the procedure with the correct argument, as shown below.
Private Sub txtProductName_Change()
PropertyChanged "ProductName"
EventsObj.FireControlChange "ProductName" ' Invoke procedure.
End Sub
Private Sub txtUnitPrice_Change()
PropertyChanged "UnitPrice"
EventsObj.FireControlChange "UnitPrice" ' Invoke procedure.
End Sub