2011-04-01 3 views
2

개체 인스턴스의 복제를 만들려고합니다.다른 인스턴스에 이벤트 처리기 할당 복사

새 인스턴스를 만들고 속성 값을 복사해도 아무런 문제가 없지만 원래 개체 인스턴스에는 해당 이벤트에 할당 된 이벤트 처리기가 있습니다. 새 인스턴스에 이벤트 처리기를 복사하려면 어떻게해야합니까?

감사합니다 ..

다음 코드 샘플은 ...

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

언어? 플랫폼? 샘플 코드를 알려주세요. –

+0

의견을 댓글로 쓰지 말고 수정하십시오. –

+0

[원본 개체의 이벤트를 완전 복제 된 복제본에 첨부하는 방법] 가능한 복제본 (http://stackoverflow.com/questions/4038465/how-to-attach-the-events-of-an-original-object -to-a-deep-copied-clone) – Breeze

답변

1

이 오래된 '빌어 먹을, 내가 알고,하지만 난이 사람이 대답을하지 않았다 믿을 수 없어, 그것은 전에 SO에 대답했습니다, right here.

한 가지만 있습니다. 주어진 예제 코드에서 sourceObject에 연결된 이벤트 처리기가없는 경우 miHandlerNothing이 될 것이므로 테스트해야합니다.