如何从子窗体检查另一个窗体是否正在其 MDI 父窗体中运行?
我有一个 MDI 表单。我想检查此表单的正在运行的子表单是否正在运行另一个表单。类似于:
if (this.MdiParent.MdiChildren.Contains(MyForm2))
{
//Do Stuff
}
其中 MyForm2
是我正在查找的表单的名称(类名称)。编译器会说“类名此时无效”。
如何正确地做到这一点?请注意,我可以在该时刻运行多个“MyForm2”实例(嗯,具有不同的实例名称!)
I have a MDI form. I want to check within a running child of this form if another form is running. Something like:
if (this.MdiParent.MdiChildren.Contains(MyForm2))
{
//Do Stuff
}
Where MyForm2
is the name (class name) for the form I am looking for. The compiler says something like "Class name is not valid at this point".
How to do this properly? Please note that I can have multiple instances of "MyForm2" running at that momemnt (Well, with different instance names!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需创建一个循环来循环访问 MdiChildren 集合,以查看是否存在指定 Type 的任何形式。包含需要特定实例才能返回有效数据:
Just create a loop to cycle through the MdiChildren collection to see if any form of the specified Type exists. Contains requires a specific instance to return valid data:
您需要检查每个孩子的类型。
例如,您可以使用
is
关键字 (更多信息)来确定子级是否是正确的类型:.Any()
方法需要引用System.Linq
。 了解有关 Any() 的更多信息You need to check the type of each child.
For example you can use the
is
keyword (more info) to determine if a child is the correct type:The
.Any()
method requires a reference toSystem.Linq
. Read more about Any()