将 AddressOf 传递给 VB.NET 中的函数以使用 AddHandler
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16740205/
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
Passing AddressOf to a function in VB.NET to use AddHandler
提问by DieSlower
I need to pass a reference of a function to another function in VB.NET. How can this be done?
我需要将一个函数的引用传递给 VB.NET 中的另一个函数。如何才能做到这一点?
My function needs to use AddHandler internally, for which I need to pass it a handling function. My code below obviously does not work, but it conveys the idea of what I need.
我的函数需要在内部使用 AddHandler,为此我需要向它传递一个处理函数。我下面的代码显然不起作用,但它传达了我需要的想法。
Public Function CreateMenuItem(ByVal Name As String, ByRef Func As AddressOf ) As MenuItem
Dim item As New MenuItem
item.Name = Name
'item. other options
AddHandler item.Click, AddressOf Func
Return item
End Function
Is there another way to do this? The AddHandler needs to be set to a passed parameter in a function somehow...
有没有另一种方法可以做到这一点?AddHandler 需要以某种方式设置为函数中传递的参数......
回答by Fuzhou Hu
A function delegate is just what you need to do this. First you need to define the delegate somewhere in the class. Change the signature to fit your event of course.
函数委托正是您所需要的。首先,您需要在类中的某处定义委托。当然,更改签名以适合您的活动。
Public Delegate Sub MyDelegate(sender As System.Object, e As System.EventArgs)
Your function will take the delegate as an argument.
您的函数将委托作为参数。
Public Function CreateMenuItem(ByVal Name As String, del As MyDelegate) As MenuItem
''''
AddHandler item.Click, del
''''
End Function
Public Sub MyEventHandler(sender As System.Object, e As System.EventArgs)
''''
End Sub
And here's how you call the function:
这是您调用该函数的方式:
CreateMenuItem(myString, AddressOf MyEventHandler)
回答by Janez Lukan
Your second argument in the function should be of type EventHandler, and your function would then look like:
函数中的第二个参数应该是 type EventHandler,然后你的函数看起来像:
Public Function CreateMenuItem(ByVal Name As String, ByRef Func As EventHandler) As MenuItem
Dim item As New MenuItem
item.Name = Name
'item. Other options
AddHandler item.Click, Func
Return item
End Function
Now you need a method to handle those clicks:
现在您需要一种方法来处理这些点击:
Private Sub ItemClick(sender As Object, e As EventArgs)
'Do something with that click here
End Sub
And you can consume those two methods now with something like:
您现在可以使用这两种方法,例如:
Dim handler = New EventHandler(AddressOf ItemClick)
Dim i = CreateMenuItem("My item", handler)
i.PerformClick()
回答by tinstaafl
First off, event handlers have to have Subs. Secondly, AddressOfcan't be used as a type.
If the sub is in the same class, just use the sub name. If it's in another class/file, you might have to make the sub public and/or qualify it as being a member of the other class. Subs for the AddHandler clause must basically follow the pattern:
首先,事件处理程序必须有 Subs。其次,AddressOf不能作为类型使用。如果子在同一个类中,只需使用子名称。如果它在另一个类/文件中,您可能必须将子公开和/或将其限定为另一个类的成员。AddHandler 子句的子句必须基本上遵循以下模式:
Public/Private Sub MyHandler(sender As Object, e As EventArgs)
If you need different routines based on the name of the menu item, you could use one handler and call the appropriate routine based on the name of the item triggering the event.
如果根据菜单项的名称需要不同的例程,则可以使用一个处理程序并根据触发事件的项的名称调用适当的例程。

