Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding parameters to an AddHandler event in VB.NET 2008

Tags:

vb.net

I read all the current posts regarding the addition of parameters to an AddHandler event, but I could not apply them to my circumstance.

In an MDI program in VB.NET 2008 I have a module, QuickSaleModule, that calls several modal data entry forms to add orders to a table. While this is occurring, a form with a grid is opened that shows all the orders in the table. This form grid has been opened separately from the order module, so that neither the form nor the module are dependent on each other though they are part of the same project in the solution. The grid of orders comes from an SQL query.

In the module I have defined the simple event:

Public event RefreshGrid()

I raise it at a certain point in the module – after an order has been entered and saved:

RaiseEvent RefreshGrid()

Now in the order grid form I have in the load event:

AddHandler QuickSaleModule.RefreshGrid, AddressOf RefreshMyGrid

And for RefreshMyGrid() handler I have:

Public Sub RefreshMyGrid()
     DoReturnSetup() – a sub in the grid form
     removeHandler QuickSaleModule.RefreshGrid, AddressOf RefreshMyGrid
End Sub

This calls the SQL query to refresh the gridform with the new order. A number of orders may be added through the quickSaleModule before it is exited, so each time a new one is entered I would call the event, so it did not make sense to remove the handler in form closed. However, this is the first AddHandler event I have created.

Here are my questions:

  1. Most important. How can I pass the orderID that is in the module as a parameter to the RefreshMyGrid handler in the grid form? I guess I could use a global variable, but I would prefer something better. Now as an event novice, it did not seem that I could use the other method of calling an event. I use the with events (with the arguments) when I exit a dataentry form that has been called from a grid form. But in this case as I said, there is no connection.

  2. Is the location of "removeHandler QuickSaleModule.RefreshGrid, AddressOf RefreshMyGrid" correct? Or should it be in the form closed event.

  3. Finally, does RefreshMyGrid need to be public?

like image 658
smh Avatar asked Jan 07 '13 01:01

smh


1 Answers

To pass arguments to an event, you should follow the .NET event standard of passing two parameters to the event: the sender and the event arguments.

In order to pass usable data, you will need to create your own class that inherits from EventArgs and then add your properties to this.

For example, define the arguments class:

Public Class RefreshGridEventArgs
    Inherits EventArgs

    Public Property OrderId As Integer

    Public Sub New(wOrderId As Integer)
        MyBase.New()

        Me.OrderId = wOrderId
    End Sub
End Class

Then redefine the event:

Public Event RefreshGrid(sender As Object, e As RefreshGridEventArgs)

Next, redefine how this event is raised:

    ' Replace 1 with the actual order id
    RaiseEvent RefreshGrid(Me, New RefreshGridEventArgs(1))

And finally, in the event handler, use e.OrderId to perform the appropriate logic.

To answer your additional questions:

2) Removing the handler within the event means that the event will only be raised once, which is not usually what you want (there are cases where this makes sense, but this doesn't seem to be one). There is also no need to remove the event handler when the form is closed since it is contained within the form, but it wouldn't hurt to do it either in case the form doesn't actually close for some reason (something is hanging onto a reference to it somewhere).

3) RefreshMyGrid does not need to be public unless it is legal for external callers to call it.

like image 186
competent_tech Avatar answered Oct 19 '22 23:10

competent_tech