にはBindingList
これらの変更をキャッチするイベントがありますが、INotifyPropertyChangedインターフェイスを実装するためにEquipmentクラスが必要になります。
Public Class Equipment
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, _
ByVal e As PropertyChangedEventArgs) _
Implements INotifyPropertyChanged.PropertyChanged
Private _Calculation As Decimal
Public Sub Calculate(ByVal newNumber As Decimal)
Me.Calculation = newNumber
End Sub
Property Calculation() As Decimal
Get
Return _Calculation
End Get
Set(ByVal value As Decimal)
If value <> _Calculation Then
_Calculation = value
RaiseEvent PropertyChanged(Me, _
New PropertyChangedEventArgs("Calculation"))
End If
End Set
End Property
End Class
EquipmentCollectionクラスは、変更されたイベントをリッスンします。
Public Class EquipmentCollection
Private WithEvents _EquipmentList As New BindingList(Of Equipment)
Public ReadOnly Property EquipmentList() As BindingList(Of Equipment)
Get
Return _EquipmentList
End Get
End Property
Private Sub EquipmentList_ListChanged(ByVal sender As Object, _
ByVal e As ListChangedEventArgs) _
Handles _EquipmentList.ListChanged
If e.ListChangedType = ListChangedType.ItemChanged Then
If e.PropertyDescriptor IsNot Nothing AndAlso _
e.PropertyDescriptor.Name = "Calculation" Then
MessageBox.Show("New Calculation = " & _
_EquipmentList.Item(e.NewIndex).Calculation.ToString)
End If
End If
End Sub
End Class
簡単な実装:
Dim ec As New EquipmentCollection
ec.EquipmentList.Add(New Equipment)
ec.EquipmentList.Add(New Equipment)
ec.EquipmentList.Last.Calculate(110.5)