Copy event handler assignment to another instance
I am trying to create a clone of an object instance.
Creating the new instance and copying property values is no problem but the original object instance has some event handlers assigned to its events. How can I copy event handlers to the new instance?
Thanks..
Here is a code sample...
Public Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(sender.Name + "was clicked")
End Sub
Public Sub CloneButton()
Dim newButton = New Button
newButton.Name = Button1.Name + "_Clone"
newButton.Text = Button1.Text
newButton.Width = Button1.Width
newButton.Height = Button1.Height开发者_如何学编程
'Some code here to copy Button1's event handler ButtonClick,
'so when the new button is clicked "Button1_Clone was clicked" is displayed.
End Sub
This is freakin' old, I know, but I can't believe this guy didn't get an answer, it's been answered on SO before, right here.
Just one thing; in the example code given, miHandler
will be Nothing
if there is no event handler attached to the sourceObject
, you should test for that.
精彩评论