如何从子窗体检查另一个窗体是否正在其 MDI 父窗体中运行?

发布于 2024-12-23 13:24:34 字数 287 浏览 8 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2024-12-30 13:24:35

只需创建一个循环来循环访问 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.
            }
        }

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:

        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.
            }
        }
素年丶 2024-12-30 13:24:35

您需要检查每个孩子的类型。

例如,您可以使用 is 关键字 (更多信息)来确定子级是否是正确的类型:

if (this.MdiParent.MdiChildren.Any(child => child is MyForm2))
{
}

.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:

if (this.MdiParent.MdiChildren.Any(child => child is MyForm2))
{
}

The .Any() method requires a reference to System.Linq. Read more about Any()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文