当此类在字典中时,尝试在 MainForm 中接收在类中引发的事件。以下是一些代码示例。
创建了一个类:
Public Class Zone
Public _ZoneID As Integer
Public _ZoneName As String
Public Event ZoneEntered(ByVal intToolID As Integer, ByVal intZoneID As Integer)
Public Sub New()
End Sub
Public Property ZoneName() As String
Get
Return _ZoneName
End Get
Set(value As String)
_ZoneName = value
End Set
End Property
Public Property ZoneID() As Integer
Get
Return _ZoneID
End Get
Set(value As Integer)
_ZoneID = value
End Set
End Property
Public Sub CheckZone(ByVal intToolID As Integer)
If intToolID > 0 Then
RaiseEvent ZoneEntered(intToolID, _ZoneID)
End If
End Sub
结束类
在 FormMain 代码中,我们有:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Zones As New Dictionary(Of Integer, Zone) 'Holds all the Zones for all CameraGroups within this Workspace
Dim NewZone As Zone
NewZone.ZoneName = "TestZone"
NewZone.ZoneID = 123
Zones.Add(1, NewZone)
Dim intZoneID As Integer = 1
If Zones.ContainsKey(intZoneID) Then
Dim ZoneActive As Zone
ZoneActive = Zones(intZoneID)
If Not (ZoneActive Is Nothing) Then
ZoneActive.CheckZone(intZoneID) 'This would fire the event I am looking for
End If
End If
End Sub
如何使用作为字典一部分的类设置事件?