问题
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.
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)
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
I'm looking for a solution that could look like this in Javascript:
onlick="clicked(this.id)"
Thank you
回答1:
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.
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
EDIT:
In the comments you ask how to pass extra parameters to event handling methods. To that, the short answer is no.
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.
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
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
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.
ADDITIONAL NOTE:
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.
来源:https://stackoverflow.com/questions/23139473/how-to-use-pass-parameters-to-event-handlers