2012-03-21 5 views
1

폼 내에 탑재 된 컨트롤이 있습니다. 컨트롤이 부모 폼을 만들 수 있도록 콜백을 구현해야합니다. 내 계획은 MustOverride 메서드를 사용하여 MustInherit 클래스를 만들고 폼이 MustInherit 클래스를 상속 받도록 만드는 것이 었습니다. 그러나 Visual Basic은 폼이 둘 이상의 클래스를 상속 할 수 없다는 것을 알려줍니다. 즉, 내 MustInherit 클래스 또는 System.Windows.Forms.Form 중 하나를 상속 받거나 둘 다 상속받을 수 있습니다.Visual Basic : 폼에서 특정 메서드를 구현하도록합니다.

private Parent as iRiksProjectParent 

public sub AttachParent(ByRef parent as iRiksProjectParent) 
    Parent = parent 
End Sub 

...

private sub ProcessData() 
    dim theProcessedData as String 
    ... 
    parent.DoSomethingWithTheData(theProcessedData) 
End Sub 

가 어떻게이 작업을 수행 할 수 있습니다

나는 내 컨트롤에 다음과 같은 일을 할 수 있도록하고 싶습니다?

답변

3

: MustInherit 클래스를 System.Windows.Forms.Form에서 상속받을 수 있습니다. 그런 다음 양식이 사용자 정의 클래스에서 상속되면 자동으로 System.Windows.Forms.Form에서 상속됩니다. 명시 적 콜백을 사용하지 마십시오 :


가 문제를 해결합니다. 간단하게 사용자 지정 컨트롤이 이벤트를 발생시킬 있습니다

Public Event DoSomethingWithTheData(processedData As String) 

Private Sub ProcessData() 
    Dim theProcessedData as String 
    ... 
    RaiseEvent DoSomethingWithTheData(theProcessedData) 
End Sub 

그 방법은, 양식은 단순히 이벤트를 처리 할 수 ​​

Private Sub myCustomControl_DoSomethingWithTheData(processedData As String) _ 
    Handles myCustomControl.DoSomethingWithTheData 

    ... 
End Sub 
+1

이벤트가 확실히 갈 수있는 방법입니다. 그리고 컨트롤에 특정 이벤트가 필요하면 인터페이스를 구현할 수 있습니다. 일반적으로 다중 상속이 필요하다고 생각 될 때 원하는 인터페이스를 구현할 수 있습니다. –

+0

환상적이며, 바로 그 직업! – RikSaunderson