Visual Basic - Handle events for class within a list

落爺英雄遲暮 提交于 2019-12-11 18:29:06

问题


Public Class EquipmentCollection
{
Public Property EquipmentList As List(Of Equipment)
}

Public Class Equipment
{
 Public Event CalculateFired

 Public Sub Calculate
  RaiseEvent CalculateFired
 End Sub
}

How can I handle the CalculateFired event on the Equipment class within the EquipmentCollection class?

.NET 3.5, VB


回答1:


This worked for me.

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



回答2:


The BindingList has events to catch those changes, but it would require your Equipment class to implement the INotifyPropertyChanged interface:

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

Your EquipmentCollection class would listen for the changed event:

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

Simple implementation:

Dim ec As New EquipmentCollection
ec.EquipmentList.Add(New Equipment)
ec.EquipmentList.Add(New Equipment)
ec.EquipmentList.Last.Calculate(110.5)



回答3:


You will need to add an event handler in EquipmentCollection to each object in the equipment list you want to handle the event for.

See the MSDN page on events and event handlers. http://msdn.microsoft.com/en-us/library/2z7x8ys3%28v=vs.80%29.aspx



来源:https://stackoverflow.com/questions/12626780/visual-basic-handle-events-for-class-within-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!