我有一个 MDI 表单。如果另一个表单正在运行,我想检查此表单的正在运行的子项。像这样的东西:
if (this.MdiParent.MdiChildren.Contains(MyForm2))
{
//Do Stuff
}
其中 MyForm2 是我要查找的表单的名称(类名)。编译器说类似“此时类名无效”。
如何正确地做到这一点?请注意,我可以同时运行多个“MyForm2”实例(好吧,使用不同的实例名称!)
请您参考如下方法:
只需创建一个循环来循环遍历 MdiChildren 集合,以查看是否存在指定 Type 的任何形式。包含需要特定实例返回有效数据:
foreach (var form in this.MdiParent.MdiChildren)
{
if (form is MyForm2)
{
// Do something.
// If you only want to see if one instance of the form is running,
// add a break after doing something.
// If you want to do something with each instance of the form,
// just keep doing something in this loop.
}
}
