6

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?

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I have put in the code to test but one problem is that I am raising the event inside a Module that cannot be referenced by 'me' so how would I do: RaiseEvent RefreshGrid(??, New RefreshGridEventArgs(1))
In that case, just send Nothing as the sender.
Well, I just removed the first argument and just used the REfreshGridEventArgs in the definition, the raiseevent and the handler. It worked. I found nothing as clear and straightforward an answser as this one. This is an easy event yet I got no good answer to the question until now. Wonderful. Thanks a bundle.
Glad I could help and welcome to stackoverflow. Keep in mind that if an answer solves or helps solves your question, you should click the checkmark next to the answer to indicate to future viewers of the question that this was the answer that worked for you. In addition, once you have enough reputation, you should also "upvote" the answer by clicking the up arrow next to answer. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.