Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net VisualBasic:强制窗体实现某些方法_Vb.net_Inheritance - Fatal编程技术网

Vb.net VisualBasic:强制窗体实现某些方法

Vb.net VisualBasic:强制窗体实现某些方法,vb.net,inheritance,Vb.net,Inheritance,我有一个安装在表单中的控件。我需要实现一个回调,以便控件可以使父窗体执行某些操作。我的计划是用方法创建一个类,并使表单继承MustInherit类。但是,Visual Basic告诉我表单不能继承多个类,这意味着它可以继承my MustInherit类或System.Windows.Forms.form,但不能同时继承这两个类 我希望能够在我的控制下执行以下操作: private Parent as iRiksProjectParent public sub AttachParent(ByRe

我有一个安装在表单中的控件。我需要实现一个回调,以便控件可以使父窗体执行某些操作。我的计划是用方法创建一个类,并使表单继承MustInherit类。但是,Visual Basic告诉我表单不能继承多个类,这意味着它可以继承my MustInherit类或System.Windows.Forms.form,但不能同时继承这两个类

我希望能够在我的控制下执行以下操作:

private Parent as iRiksProjectParent

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

我该怎么做?

直接回答您的问题:您可以让您的
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

事件无疑是一条路要走。如果您需要要求您的控件具有特定事件,那么您可以让它实现一个接口——当您认为需要多重继承时,这通常是您想要的。
Private Sub myCustomControl_DoSomethingWithTheData(processedData As String) _
    Handles myCustomControl.DoSomethingWithTheData

    ...
End Sub