Pieter,
Here's a .NET 2.0 sample (VS 2005):
It uses System.Collections.ObjectModel.Collection(Of T) for the base of
the
collection. Collection(Of T) is the replacement for CollectionBase, just
as
List(Of T) is the replacement for ArrayList.
---x--- cut here ---x---
'
' Copyright © 2005, Jay B. Harlow, All Rights Reserved.
'
Option Strict On
Option Explicit On
Public Class Person
Public Event NameChanged As EventHandler
Private ReadOnly m_id As Integer
Private m_name As String
Public Sub New(ByVal id As Integer)
m_id = id
End Sub
Public ReadOnly Property Id() As Integer
Get
Return m_id
End Get
End Property
Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
OnNameChanged(EventArgs.Empty)
End Set
End Property
Protected Sub OnNameChanged(ByVal e As EventArgs)
RaiseEvent NameChanged(Me, e)
End Sub
Public Overrides Function ToString() As String
Return "Person(" & m_id & ", " & m_name & ")"
End Function
End Class
---x--- cut here ---x---
'
' Copyright © 2005, Jay B. Harlow, All Rights Reserved.
'
Option Strict On
Option Explicit On
Public Class PersonCollection
Inherits System.Collections.ObjectModel.Collection(Of Person)
Public Event PersonNameChanged As EventHandler(Of
PersonNameChangedEventArgs)
Protected Overrides Sub ClearItems()
For Each item As Person In Me
RemoveHandler item.NameChanged, AddressOf OnNameChanged
Next
MyBase.ClearItems()
End Sub
Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item
As
Person)
AddHandler item.NameChanged, AddressOf OnNameChanged
MyBase.InsertItem(index, item)
End Sub
Protected Overrides Sub RemoveItem(ByVal index As Integer)
Dim item As Person = Me(index)
RemoveHandler item.NameChanged, AddressOf OnNameChanged
MyBase.RemoveItem(index)
End Sub
Protected Overrides Sub SetItem(ByVal index As Integer, ByVal item As
Person)
Dim itemOriginal As Person = Me(index)
RemoveHandler itemOriginal.NameChanged, AddressOf OnNameChanged
AddHandler item.NameChanged, AddressOf OnNameChanged
MyBase.SetItem(index, item)
End Sub
Private Sub OnNameChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim args As New PersonNameChangedEventArgs(DirectCast(sender,
Person))
OnPersonNameChanged(args)
End Sub
Protected Overridable Sub OnPersonNameChanged(ByVal e As
PersonNameChangedEventArgs)
RaiseEvent PersonNameChanged(Me, e)
End Sub
End Class
---x--- cut here ---x---
'
' Copyright © 2005, Jay B. Harlow, All Rights Reserved.
'
Option Strict On
Option Explicit On
Public Class PersonNameChangedEventArgs
Inherits EventArgs
Private ReadOnly m_person As Person
Public Sub New(ByVal person As Person)
m_person = person
End Sub
Public ReadOnly Property Person() As Person
Get
Return m_person
End Get
End Property
End Class
---x--- cut here ---x---
'
' Copyright © 2005, Jay B. Harlow, All Rights Reserved.
'
Option Strict On
Option Explicit On
Public Module MainModule
Private WithEvents people As New PersonCollection
Public Sub Main()
Dim person1 As New Person(1)
Dim person2 As New Person(2)
Dim person3 As New Person(3)
people.Add(person1)
people.Add(person2)
people.Add(person3)
person1.Name = "Person 1"
person2.Name = "Person 2"
person3.Name = "Person 3"
people.Remove(person3)
person3.Name = String.Empty
people(0) = person3
person1.Name = String.Empty
person3.Name = "Jay"
people.Clear()
person1.Name = String.Empty
person2.Name = String.Empty
person3.Name = String.Empty
End Sub
Private Sub People_PersonNameChanged(ByVal sender As Object, ByVal e As
PersonNameChangedEventArgs) Handles people.PersonNameChanged
Debug.WriteLine(e.Person, "Person Name Changed")
End Sub
End Module
---x--- cut here ---x---
--
Hope this helps
Jay [MVP - Outlook]
T.S. Bradley -
http://www.tsbradley.net
| Thanks! That's a great idea!
|
| message | > Pieter,
| > As Herfreid stated, your clsPersonne raises events List(Of T) does
not.
| >
| > It sounds like you want List(Of T) to raise events based on the
objects
it
| > contains. What I normally do is derive a class from the base
collection
| > (List(Of T) in your case), then override the "add" and "remove" of the
| > collection to use AddHandler & RemoveHandler on each object being
added
or
| > removed from my collection, so that the collection can raise the event
| > that
| > the individual items raise...
| >
<<snip>>