向 VB.NET 2008 中的 AddHandler 事件添加参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14188525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding parameters to an AddHandler event in VB.NET 2008
提问by smh
I read all the current posts regarding the addition of parameters to an AddHandler event, but I could not apply them to my circumstance.
我阅读了所有当前关于向 AddHandler 事件添加参数的帖子,但我无法将它们应用于我的情况。
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.
在 VB.NET 2008 中的 MDI 程序中,我有一个模块 QuickSaleModule,它调用多个模式数据输入表单以将订单添加到表中。发生这种情况时,会打开一个带有网格的表格,显示表格中的所有订单。此表单网格已与订单模块分开打开,因此表单和模块都不是相互依赖的,尽管它们是解决方案中同一项目的一部分。订单网格来自 SQL 查询。
In the module I have defined the simple event:
在模块中,我定义了简单事件:
Public event RefreshGrid()
公共事件 RefreshGrid()
I raise it at a certain point in the module – after an order has been entered and saved:
我在模块中的某个点提出它 - 在输入并保存订单后:
RaiseEvent RefreshGrid()
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:
对于 RefreshMyGrid() 处理程序,我有:
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.
这将调用 SQL 查询以使用新订单刷新 gridform。在退出之前,可以通过 quickSaleModule 添加许多订单,因此每次输入新订单时,我都会调用该事件,因此在关闭表单中删除处理程序没有意义。但是,这是我创建的第一个 AddHandler 事件。
Here are my questions:
以下是我的问题:
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.
Is the location of "removeHandler QuickSaleModule.RefreshGrid, AddressOf RefreshMyGrid" correct? Or should it be in the form closed event.
Finally, does RefreshMyGrid need to be public?
最重要的。如何将模块中的 orderID 作为参数传递给网格表单中的 RefreshMyGrid 处理程序?我想我可以使用全局变量,但我更喜欢更好的东西。现在作为一个事件新手,我似乎无法使用其他调用事件的方法。当我退出从网格表单调用的数据输入表单时,我使用 with 事件(带参数)。但在这种情况下,正如我所说,没有任何联系。
“removeHandler QuickSaleModule.RefreshGrid, AddressOf RefreshMyGrid”的位置是否正确?或者它应该以关闭事件的形式出现。
最后,RefreshMyGrid 需要公开吗?
回答by competent_tech
To pass arguments to an event, you should follow the .NETevent standard of passing two parameters to the event: the sender and the event arguments.
要将参数传递给事件,您应该遵循将两个参数传递给事件的.NET事件标准:发送方和事件参数。
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.
为了传递可用数据,您需要创建自己的继承自 EventArgs 的类,然后将您的属性添加到该类中。
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.OrderIdto perform the appropriate logic.
最后,在事件处理程序中,使用e.OrderId来执行适当的逻辑。
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).
2)删除事件中的处理程序意味着该事件只会引发一次,这通常不是您想要的(在某些情况下这是有道理的,但这似乎不是一个)。当表单关闭时也不需要删除事件处理程序,因为它包含在表单中,但是如果表单由于某种原因实际上没有关闭(某些东西挂在上面),这样做也没有什么坏处在某处引用它)。
3) RefreshMyGrid does not need to be public unless it is legal for external callers to call it.
3) RefreshMyGrid 不需要公开,除非外部调用者调用它是合法的。

