如何使用/将参数传递给事件处理程序? [英] How to use / pass parameters to event handlers?
问题描述
我已经找到了有关此主题的一些信息,但是我仍然没有完全理解它,如果有人在我的示例中向我展示如何做到这一点,我将非常高兴.
I already found some information about this topic, but I still don't fully understand it and would be happy if someone would show me how to do it on my example.
基本上,我在VB.Net Windows窗体应用程序中有一个充满对象的数组
Basically, I've got an array filled with objects in VB.Net Windows Forms Application
Dim button(9) As cbuttons
public sub fill()
button(1) = New cbuttons("buttonName1", 2, true)
button(2) = New cbuttons("ButtonName2", 3, true)
button(3) = New cbuttons("ButtonName3", 4, true)
...
此数组用作动态生成的按钮的源
This array serves as a source for buttons, which are dynamically generated
public sub writeButtons()
For Each item As cbutton In button
Dim cmbDynamic As New Button()
cmbDynamic.Location = New System.Drawing.Point(item.getX(), item.getY())
cmbDynamic.Name = item.getname()
cmbDynamic.Text = item.getName() & vbCrLf & item.getTypeString()
TabPage1.Controls.Add(cmbDynamic)
AddHandler cmbDynamic.Click, AddressOf Me.clicked 'here is the problem
然后我有一个潜水艇,应该执行类似的操作
Then I've got a sub that should do something like this
public sub clicked()
MessageBox.Show("You clicked on button " & ButtonName)
但是,我不知道如何将按钮名称(以及其他任何参数)传递给子 clicked(),因为我无法将任何参数添加到 AddHalnder 行和 callByName()也不起作用
However, I have no idea how to pass the button name (nor any other parameters) to the sub clicked(), because I can't add any parameter to the AddHalnder line and callByName() also doesn't work
我正在寻找一个在Javascript中看起来像这样的解决方案:
I'm looking for a solution that could look like this in Javascript:
onlick="clicked(this.id)"
谢谢
推荐答案
该方法必须具有与Control.Click
事件委托匹配的参数,本例中为EventHandler
.因此,clicked
方法需要一个Object
类型参数,后跟EventArgs
类型参数.
The method must have parameters matching the Control.Click
event delegate, which in this case is EventHandler
. Therefore, clicked
method needs an Object
type parameter followed by EventArgs
type parameter.
第一个参数表示发生事件的对象.因此,在将其转换为处理程序之后,您将能够获取其任何属性,包括Name
:
The first parameter represents the object for which the event occured. Therefore, upon casting it within the handler you will be able to get any of its properties, including Name
:
Public Sub clicked(sender As Object, e As EventArgs)
MessageBox.Show("You clicked on button " & CType(sender, Control).Name)
End Sub
在注释中,您询问如何将额外的参数传递给事件处理方法.对此,简短的答案是否".
In the comments you ask how to pass extra parameters to event handling methods. To that, the short answer is no.
但是,有一些解决方法.您可以将值存储在类范围内,并在处理程序中访问它们.您还可以将信息存储在每个Button
的Tag
属性中.由于处理程序始终有一个Object
参数来表示引发事件的对象,因此您可以在其上下文中检索其任何属性.
However, there are ways around this. You could store values within class scope and access them in the handler. You could also store information in each Button
's Tag
property. Since the handler always have an Object
parameter representing the object raising the event, you can retrieve any of its properties within its context.
例如,当您创建一个新的Button
实例时,可以将其Tag
属性设置为创建该实例的cbutton
值:
For example, when you create a new Button
instance, you can set its Tag
property to the cbutton
value from which it is being created:
Public Sub writeButtons()
' ...
Dim cmbDynamic As New Button()
' ...
cmbDynamic.Tag = item
' ...
End Sub
因此,稍后,您只需执行以下操作即可恢复完整的cbutton
值:
So, later on, you'll be able to recover the full cbutton
value simply by doing this:
Public Sub clicked(sender As Object, e As EventArgs)
Dim btn As Button = sender
Dim item As cbutton = btn.Tag
MessageBox.Show("You clicked on button " & item.getname() & " which has cbutton.anyproperty equals to " & item.anyproperty)
End Sub
您可以将Tag
属性视为自由式存储.忠告之词:不要滥用它.您使用它的次数越多,您的代码就越难弄清楚.另外,如果您有多个使用它们的机械师,那么您可能最终会得到其中的意外值.
You can think of Tag
property as freestyle storage. Word of advise though: do not abuse of it. The more you use it, the harder your code will be to figure out. Plus, you might end up having unexpected values in these should you have more than one mechanic that uses them.
附加说明:
根据.NET约定,类型和类型成员的首选大小写形式是每个单词都以大写字母开头.例如,writeButtons
实际上应该是WriteButtons
.我还应该提到以Write
开头的方法表明I/O操作正在发生.就您而言,CreateButtons
听起来更合适.
As per .NET conventions, the preferred casing for types and type members is for every words to start with a capital letter. For example, writeButtons
really should be WriteButtons
. I should also mention that methods starting with Write
suggests I/O operations happening. In your case, CreateButtons
sounds more appropriate.
这篇关于如何使用/将参数传递给事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!