在 eterogene MdiChild FormVB.NET 上调用相同的函数

Calling the same function on eterogene MdiChild FormVB.NET

我的 MDIParent 中有一个 eterogene MDIChildForms(MyChild1、MyChild2、...)列表。所有这种形式都有一个 public 函数 myFunction(p As myType)。我想循环它们并为每个调用 myFunction。

伪代码:

For Each myChild In Me.MdiChildren
     myChild.myFuntion(p)
Next

可能吗?我怎样才能做到这一点?谢谢

您应该定义一个声明该方法的接口,然后在每个子窗体中实现该接口 class。

Public Interface ISomeInterface

    Sub SomeMethod()

End Interface
Public Class Form1
    Implements ISomeInterface

    Public Sub SomeMethod() Implements ISomeInterface.SomeMethod
        '...
    End Sub

End Class

然后您可以将每个表单转换为该类型并调用该方法,例如

For Each mdiChild As ISomeInterface In MdiChildren
    mdiChild.SomeMethod()
Next

假设每个表单都实现了该接口。如果有些人不这样做,你可以这样做:

For Each mdiChild In MdiChildren.OfType(Of ISomeInterface)()
    mdiChild.SomeMethod()
Next