You can only declare a variable WithEvents when its type has events. Just because Class2 has events doesn't mean that a Class2 array has those events. Does an egg carton magically take on the properties of an egg because it is used to store eggs? You need to use an AddHandler statement to attach a method to the desired event of each Class2 object. That could be done in the application or it could be done in Class1. That could allow you to then raise an event in Class1 that could be handled just like any other event of Class1. Here's an example of how such an arrangement could be implemented.
Public Class Child
Private _text As String
Public Property Text() As String
Get
Return _text
End Get
Set(value As String)
If _text <> value Then
_text = value
OnTextChanged(EventArgs.Empty)
End If
End Set
End Property
Public Event TextChanged As EventHandler
Protected Overridable Sub OnTextChanged(e As EventArgs)
RaiseEvent TextChanged(Me, e)
End Sub
End Class
Public Class Parent
Public Class ChildCollection
Inherits Collection(Of Child)
Private ReadOnly childTextChangedEventHandler As EventHandler(Of ChildEventArgs)
Public Sub New(childTextChangedEventHandler As EventHandler(Of ChildEventArgs))
Me.childTextChangedEventHandler = childTextChangedEventHandler
End Sub
Protected Overrides Sub InsertItem(index As Integer, item As Child)
AddHandler item.TextChanged, childTextChangedEventHandler
MyBase.InsertItem(index, item)
End Sub
Protected Overrides Sub SetItem(index As Integer, item As Child)
Dim currentItem = Items(index)
RemoveHandler currentItem.TextChanged, childTextChangedEventHandler
AddHandler item.TextChanged, childTextChangedEventHandler
MyBase.SetItem(index, item)
End Sub
Protected Overrides Sub RemoveItem(index As Integer)
Dim currentItem = Items(index)
RemoveHandler currentItem.TextChanged, childTextChangedEventHandler
MyBase.RemoveItem(index)
End Sub
Protected Overrides Sub ClearItems()
For Each item As Child In Items
RemoveHandler item.TextChanged, childTextChangedEventHandler
Next
End Sub
End Class
Private _text As String
Private ReadOnly _children As New ChildCollection(AddressOf Children_TextChanged)
Public Property Text() As String
Get
Return _text
End Get
Set(value As String)
If _text <> value Then
_text = value
OnTextChanged(EventArgs.Empty)
End If
End Set
End Property
Public ReadOnly Property Children As ChildCollection
Get
Return _children
End Get
End Property
Public Event TextChanged As EventHandler
Public Event ChildTextChanged As EventHandler(Of ChildEventArgs)
Protected Overridable Sub OnTextChanged(e As EventArgs)
RaiseEvent TextChanged(Me, e)
End Sub
Protected Overridable Sub OnChildTextChanged(e As ChildEventArgs)
RaiseEvent ChildTextChanged(Me, e)
End Sub
Private Sub Children_TextChanged(sender As Object, e As EventArgs)
OnChildTextChanged(New ChildEventArgs(DirectCast(sender, Child)))
End Sub
End Class
Public Class ChildEventArgs
Inherits EventArgs
Private _child As Child
Public ReadOnly Property Child As Child
Get
Return _child
End Get
End Property
Public Sub New(child As Child)
_child = child
End Sub
End Class
If I haven't messed that up, you can now just declare a Parent variable WithEvents and handle its TextChanged and ChildTextChanged events. When a Child raises its TextChanged event, the Parent raises its ChildTextChanged event and you can then access the Child that raised the original event via the e.Child property.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.