Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a form in VB.NET that is used as a dialog in a mainform. Its instances are always locally defined, there's no field for it. When the user clicks the OK button in the dialog, it will fire an event with exactly one argument, an instance of one of my classes.

Since it is always a local variable, how can I add an event handler for that event? I've searched for myself and found something but I can't really figure it out...

Code for the event, a field in MyDialog:

public Event ObjectCreated(ByRef newMyObject as MyObject)

Code for the main form to call dialog : (never mind 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'm stuck on how to add a handler for my event. Can anyone help me? Preferrably with the best way to do it...

like image 968
MarioDS Avatar asked Apr 25 '12 15:04

MarioDS


People also ask

How do you create an event handler in Visual Basic?

To create an event handler in Visual BasicFrom the Class Name drop-down list at the top of the Code Editor, select the object that you want to create an event handler for. From the Method Name drop-down list at the top of the Code Editor, select the event.

How do you call an event handler in VB net?

Call an event handler using AddHandlerExecute an AddHandler statement to dynamically connect the event-handling Sub procedure with the event. When the event occurs, Visual Basic automatically calls the Sub procedure. Your code can use a RaiseEvent statement to make the event occur.

What is event in Visual Basic?

An event is a signal that informs an application that something important has occurred. For example, when a user clicks a control on a form, the form can raise a Click event and call a procedure that handles the event.

What is sender As object in VB net?

The sender parameter will reveal which Textbox was clicked. Private Sub FindIt( ByVal sender As System.Object, ByVal e As System.EventArgs. ) Handles TextBox1.


1 Answers

It's recommended, for consistency, that you use the same source and event args model as all system event handlers.

Create your own class inheriting from EventArgs, as:

Public Class MyObjectEventArgs
    Inherits EventArgs

    Public Property EventObject As MyObject

End Class

Then declare your event, and a handler method, like:

Public Event ObjectCreated As EventHandler(Of MyObjectEventArgs)

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

Then attach the handler to your event using:

AddHandler ObjectCreated, AddressOf Container_ObjectCreated

Additionally, you can use the Handles to attach to the event raised from your main form (assuming the name MainForm), as below:

Private Sub MainForm_ObjectCreated(ByVal sender As Object, ByVal e As MyObjectEventArgs) Handles MainForm.ObjectCreated
    ' Handler code here
End Sub
like image 119
Mike Guthrie Avatar answered Oct 12 '22 23:10

Mike Guthrie