Reputation: 11399
I would like to set up a callback that would be raised in my class by a different class:
Public Class CameraWindow
Inherits System.Windows.Forms.Control
Private m_camera As Camera = Nothing
' Camera property
<Browsable(False)> _
Public Property Camera() As Camera
Get
Return m_camera
End Get
Set(value As Camera)
' lock
Monitor.Enter(Me)
' detach event
If m_camera IsNot Nothing Then
m_camera.NewFrame -= New EventHandler(AddressOf camera_NewFrame)
timer.[Stop]()
End If
m_camera = value
needSizeUpdate = True
firstFrame = True
flash = 0
' atach event
If m_camera IsNot Nothing Then
m_camera.NewFrame += New EventHandler(AddressOf camera_NewFrame)
timer.Start()
End If
' unlock
Monitor.[Exit](Me)
End Set
End Property
' On new frame ready
Private Sub camera_NewFrame(sender As Object, e As System.EventArgs)
Invalidate()
End Sub
The event is defined in
Public Class Camera
Public Event NewFrame As EventHandler
But VB.NET does not like the way I attach and detach the events. Can somebody tell me how to do it correctly?
Thank you very much for the help!
Upvotes: 1
Views: 1263
Reputation: 460148
That looks as if you're normally using C#. In VB.NET you use the AddHandler
statement:
AddHandler m_camera.NewFrame, AddressOf camera_NewFrame
To remove the handler use RemoveHandler
RemoveHandler m_camera.NewFrame, AddressOf camera_NewFrame
However, if the event handler is in a different class it need to be public:
Public Sub camera_NewFrame(sender As Object, e As System.EventArgs)
Invalidate()
End Sub
and you need an instance:
AddHandler Camera.NewFrame, AddressOf m_camera.camera_NewFrame
Upvotes: 4