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

问题描述

我的 MDIParent 中有一个 eterogene MDIChildForms (MyChild1,MyChild2,...) 的列表。所有这些表单都有一个公共函数 myFunction(p As myType)。我想循环它们,并为每个调用 myFunction。

代码

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

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

解决方法

您应该定义一个接口来声明该方法,然后在每个子表单类中实现该接口。

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...