我正在尝试创建一个对象实例的克隆。
创建新实例并复制属性值没有问题,但原始对象实例有一些事件处理程序分配给它的事件。如何将事件处理程序复制到新实例?
谢谢..
这是一个代码示例......
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
答案 0 :(得分:1)
我知道,这已经过时了,但是我无法相信这个家伙没有得到答案,之前已经回答过,right here。
只有一件事;在给出的示例代码中,如果没有事件处理程序附加到miHandler
,Nothing
将为sourceObject
,您应该测试它。