如何使用 VB.NET 在 AddHandler 中传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13401246/
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
How to pass parameters in AddHandler using VB.NET
提问by Will
This is what I'm trying to do. I'm creating a dynamic check with an autoback that when clicked, will go to my subroutine and do something. The two parameters I'm trying to pass is the table which the checkox is located and the name of the id of the checkbox. But I'm getting the error
这就是我正在尝试做的。我正在创建一个带有自动返回的动态检查,当单击它时,将转到我的子程序并执行某些操作。我试图传递的两个参数是复选框所在的表和复选框的 id 名称。但我收到错误
AddressOf must be the name of a method without parentheses or
method does not have a signature compatible with sender as object, e system.eventArgs". Here is my code below.
AddressOf 必须是不带括号的方法名或
方法没有与作为对象的发件人兼容的签名,e system.eventArgs”。这是我的代码如下。
chkSel = New CheckBox
chkSel.ID = "check_" & CStr(a)
chkSel.AutoPostBack = True
'This is where I get the error
AddHandler chkSel.CheckedChanged, AddressOf change_operating_items(tableName, "check_" & CStr(a))
tblcell.Controls.Add(chkSel)
tblrow.Cells.Add(tblcell)
回答by Tim Schmelter
You cannot pass arguments when you register an event handler.
注册事件处理程序时不能传递参数。
Instead you can pass them when you raise that event in case of a custom event.
相反,您可以在发生自定义事件时引发该事件时传递它们。
Here you need to handle the CheckedChangedevent, cast the Senderto CheckBoxand use it's IDproperty.
在这里,您需要处理CheckedChanged事件,将其转换Sender为CheckBox并使用它的ID属性。
Sub change_operating_items(sender As Object, e As EventArgs)
Dim chk = DirectCast(sender, CheckBox)
Dim id = chk.ID
' do something with it '
EndSub

