Public Class EquipmentCollection
{
Public Property EquipmentList As List(Of Equipment)
}
Public Class Equipment
{
Public Event CalculateFired
Public Sub Calculate
RaiseEvent CalculateFired
End Sub
}
如何处理EquipmentCollection类中的Equipment类上的CalculateFired事件?
.NET 3.5,VB
答案 0 :(得分:2)
BindingList
有事件来捕获这些更改,但它需要您的Equipment类实现INotifyPropertyChanged接口:
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)
答案 1 :(得分:0)
您需要在EquipmentCollection中为要处理事件的设备列表中的每个对象添加一个事件处理程序。
请参阅有关事件和事件处理程序的MSDN页面。 http://msdn.microsoft.com/en-us/library/2z7x8ys3%28v=vs.80%29.aspx
答案 2 :(得分:0)
这对我有用。
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ec As New EquipmentCollection
Dim eq As New Equipment
Dim el As New List(Of Equipment)
eq = New Equipment
el.Add(eq)
eq = New Equipment
el.Add(eq)
eq = New Equipment
el.Add(eq)
eq = New Equipment
el.Add(eq)
eq = New Equipment
el.Add(eq)
eq = New Equipment
el.Add(eq)
eq = New Equipment
el.Add(eq)
ec.EquipmentList = el
ec.EquipmentList.Item(2).Calculate()
End Sub
End Class
Public Class EquipmentCollection
Private WithEvents _EquipmentList As New List(Of Equipment)
Public Property EquipmentList As List(Of Equipment)
Get
Return _EquipmentList
End Get
Set(value As List(Of Equipment))
Dim counter As Integer
_EquipmentList = value
For counter = 0 To _EquipmentList.Count - 1
AddHandler _EquipmentList.Item(counter).CalculateFired, AddressOf HandleCalculateFired
Next
End Set
End Property
Private Sub HandleCalculateFired()
MsgBox("calc was fired")
End Sub
End Class
Public Class Equipment
Public Event CalculateFired()
Public Sub Calculate()
RaiseEvent CalculateFired()
End Sub
End Class