answersLogoWhite

0

It is a bit sad that the compiler doesn't generate an error for this. You have to make it look like this:

Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load

obj1 = New Class1

AddHandler Class2.TestEvent, AddressOf handleEventFromClass2

End Sub

Private Sub handleEventFromClass2(ByRef sender As Class2, _

ByVal op As Integer)

TextBox1.Text = "Wert : " & op

End Sub

Note that missing Handles keyword on the event handler and the explicit use of the AddHandler keyword. You are however going to get yourself into trouble with these Shared events. There is no mechanism that automatically unsubscribes the event, Shared makes it global so it lives for the duration of your program. Even after the user has closed the form. That's going to go bad, an ObjectDisposedException is likely be raised when you fire the event since the form object is dead. Furthermore, you have a permanent leak since the form object can't be garbage collected.

You have to explicitly unsubscribe the event:

Private Sub Form1_FormClosed(ByVal sender As Object, _

ByVal e As FormClosedEventArgs) Handles Me.FormClosed

RemoveHandler Class2.TestEvent, AddressOf handleEventFromClass2

End Sub

Event sources that outlive their listeners are troublesome.

User Avatar

Wiki User

9y ago

What else can I help you with?