can't see events

Jas91

Member
Joined
Jul 12, 2012
Messages
17
Location
Baghdad, Italy. OK?=
Programming Experience
5-10
hi! I have two classes: Class1 and Class2
and a main project where an object "C1" is declared as Class1
Class1 contains an array of Class2

such:
Dim C2() As Class2
whose bounds I redefine afterwards.

OK now, both Class1 and Class2 contain methods which raise events
but from the main project, I can only intercept events from Class1

I need intercept events from Class2 also, how can I do this? help please :D

ps: I can't declare C2 withevents, why?
 
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.
 
Back
Top