Re: event propagation

Tech-Archive recommends: Fix windows errors by optimizing your registry




"thegios" <thegios@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote
> I have a CWorkflow class, containing a Cenclosures class, which is a
> collection class of CEnclosure items.

> CEnclosure fires a GotDirty event when an enclosure's property is changed.
> Cenclosures catches the CEnclosure.GotDirty event and fires another GotDirty
> event; it fires a GotDirty event even when a new enclosure is added or an existing one is deleted.
> Cworkflow catches the CEnclosures.GotDirty event and fires another GotDirty
> event; it fires a GotDirty event even when a workflow's property is cahnged.
<...>
> The problem is hat I get only 3 message boxes, because the last one is
> missing, that is no event if fired/caught when changing a property of the just added enclosure.


It is missing because you do not have anyone listening when the event is raised.



> Public Function Add(Index As Integer, Reference As String) As CEnclosure
< ... >
> Set Add = newENC
> Set newENC = Nothing
> End Function

Note here that you set newENC to Nothing, so it no longer will be listening
for events.

Typically, if you want to raise an event from many child objects to a single
parent object, you'd do that using a third object that you can pass to all
the child objects. Each child then has a reference to the same object
delegated to raising events, and the parent only needs to listen for events
from that one delegated object. A simple example follows:

Uses 4 modules:
TYPE NAME
Form Form1
Class Parent
Class Child
Class Delegate

' [ Form1 ]

Option Explicit
Private WithEvents Mom As Parent

Private Sub Form_Load()
Set Mom = New Parent
Dim Sue As Child
Dim Ted As Child
Dim Pat As Child

Set Sue = Mom.Create("Sue", 12)
Set Ted = Mom.Create("Ted", 10)

Set Pat = New Child
Pat.Name = "Pat"
Pat.Age = 8
Mom.Add Pat ' Event raised from parent

Pat.Name = "Patty" ' Event raised from child
Sue.Age = 13 ' Event raised from child

End Sub

Private Sub Mom_Shouting(Kid As Child, Msg As String)
Debug.Print Kid.Name, Msg
End Sub



' [ Parent ]

Private Kids As New Collection
Private WithEvents ChildControl As Delegate

Public Event Shouting(Kid As Child, Msg As String)

Public Sub Add(Kid As Child)
Kids.Add Kid
Set Kid.Control = ChildControl
RaiseEvent Shouting(Kid, "Added to collection.")
End Sub

Public Function Create(Name As String, Age As Long) As Child
Set Create = New Child
Create.Name = Name
Create.Age = Age
Set Create.Control = ChildControl
End Function

Private Sub ChildControl_Shouting(Kid As Child, Msg As String)
RaiseEvent Shouting(Kid, Msg)
End Sub

Private Sub Class_Initialize()
Set ChildControl = New Delegate
End Sub


' [ Child ]

Public Control As Delegate

Private mName As String
Private mAge As Long

Public Property Get Name() As String
Name = mName
End Property

Public Property Let Name(ByVal NewName As String)
If NewName <> mName Then
mName = NewName
ReportBack "Name change."
End If
End Property

Public Property Get Age() As Long
Age = mAge
End Property

Public Property Let Age(ByVal NewAge As Long)
If NewAge <> mAge Then
mAge = NewAge
ReportBack "Age change."
End If
End Property

Private Sub ReportBack(Msg As String)
If Not Me.Control Is Nothing Then
Me.Control.Shout Me, Msg
End If
End Sub



' [ Delegate ]

Public Event Shouting(Kid As Child, Msg As String)

Public Sub Shout(Kid As Child, Msg As String)
RaiseEvent Shouting(Kid, Msg)
End Sub


.



Relevant Pages

  • RE: jpgs not showing on forms
    ... Rather than embed the pictures in the database store the paths to the JPEG ... Private Sub cmdAddImage_Click ... Dim strAdditionalTypes As String, strFileList As String ... Private Sub cmdDeleteImage_Click ...
    (microsoft.public.access.gettingstarted)
  • Re: Playing an AVI video
    ... You'd use the "window avi handle" MCI command string. ... Dim mlRet As Long ... Private Sub CenterObject ...
    (microsoft.public.vb.general.discussion)
  • Re: Multi-image Bitmap Extraction
    ... Private Sub Form_Load ... cFileName As String * MAX_PATH ... Dim WFD As WIN32_FIND_DATA, hFile As Long ...
    (microsoft.public.vb.general.discussion)
  • Re: Working Transactions somehow started not to work
    ... ÖA> Private Sub Form_Open ... ÖA> Dim strSqlMain As String ...
    (microsoft.public.access.adp.sqlserver)
  • Re: Custom textbox in Userform
    ... Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ... Function IsValid(strText As String) As Boolean ...
    (microsoft.public.excel.programming)