How to add an event handler to a local variable in VB.NET

I have a form in VB.NET that is used as a dialog in the main form. Its instances are always locally defined; there is no field for this. When the user clicks the OK button in the dialog box, he fires an event with one argument, an instance of one of my classes.

Since this is always a local variable, how can I add an event handler for this event? I searched for myself and found something, but I cannot figure it out ...

Code for the event, field in MyDialog:

public Event ObjectCreated(ByRef newMyObject as MyObject)

Code for the main form for calling the dialog: (not to mention the syntax)

Dim dialog As New MyDialog()
dialog.ShowDialog(Me)
AddHandler ObjectCreated, (what do I put here?) //Or how do I add a handler?

As you can see, I am stuck on how to add a handler for my event. Can someone help me? Preferably with the best way to do this ...

+5
2

, argt source .

, EventArgs, :

Public Class MyObjectEventArgs
    Inherits EventArgs

    Public Property EventObject As MyObject

End Class

, :

Public Event ObjectCreated As EventHandler(Of MyObjectEventArgs)

Private Sub Container_ObjectCreated(ByVal sender As Object, ByVal e As MyObjectEventArgs)
    ' Handler code here
End Sub

, :

AddHandler ObjectCreated, AddressOf Container_ObjectCreated

, Handles , ( MainForm), :

Private Sub MainForm_ObjectCreated(ByVal sender As Object, ByVal e As MyObjectEventArgs) Handles MainForm.ObjectCreated
    ' Handler code here
End Sub
+6

, :

public Sub OnObjectCreated(ByRef newMyObject as MyObject)
   ...
End Sub

:

AddHandler ObjectCreated, AddressOf OnObjectCreated

, ByRef . VB . (, int ..) ByVal ByRef

+1

All Articles