C# 表单可以判断是否有任何模态窗口打开吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/409945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:16:49  来源:igfitidea点击:

Can a form tell if there are any modal windows open?

c#.netwinforms

提问by user18046

How, being inside the main form of my WinForm app can I tell if there are any modal windows/dialogs open that belong to the main form?

如何在我的 WinForm 应用程序的主窗体中判断是否有任何属于主窗体的模式窗口/对话框打开?

回答by Juliet

Long story short: opening a modal form is blocks execution on the main form as long as the modal window is open, so your main form can never check to see if its opened any modal forms until after the modal form has closed. In other words, your question is based on a misunderstanding of how modal forms work, so its moot altogether.

长话短说:只要模态窗口打开,打开模态表单就会阻止在主表单上执行,因此您的主表单永远不会检查它是否打开了任何模态表单,直到模态表单关闭之后。换句话说,您的问题是基于对模态形式如何工作的误解,因此完全没有实际意义。

For what its worth, it ispossible to tell if there are any modal forms open:

对于什么它的价值,它可以说,如果有任何模式窗体打开:

foreach (Form f in Application.OpenForms)
{
    if (f.Modal)
    {
        // do stuff
    }
}

回答by Juliet

if (this.Visible && !this.CanFocus)
{
    // modal child windows are open
}

回答by jreichert

You maybe can use the events for EnterThreadModaland LeaveThreadModal. Here is an example how you can do it:

您也许可以将事件用于EnterThreadModalLeaveThreadModal。这是一个如何做到这一点的示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.EnterThreadModal += new EventHandler(Application_EnterThreadModal);
            Application.LeaveThreadModal += new EventHandler(Application_LeaveThreadModal);

            Application.Run(new Form1());
        }

        private static void Application_EnterThreadModal(object sender, EventArgs e)
        {
            IsModalDialogOpen = true;
        }

        private static void Application_LeaveThreadModal(object sender, EventArgs e)
        {
            IsModalDialogOpen = false;
        }

        public static bool IsModalDialogOpen { get; private set; }
    }
}

回答by SnowMan50

Timers still run and fire events.
Example that works...

计时器仍在运行并触发事件。
有效的例子...

public partial class Form1 : Form
{
    Form2 f2 = new Form2();
    public Form1()
    {
        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        f2.UpdateData(DateTime.Now.ToString());
        if (!f2.Visible) f2.ShowDialog();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        f2.ShowDialog();
        MessageBox.Show("Done");
    }
}

回答by SnowMan50

If you Google a bit you will find that Form.ShowDialog() disables other forms to prevent user input to those forms of the current one. But most everything else (like timers and other events from sources external to the displayed form) continues to run.

如果你谷歌一下,你会发现 Form.ShowDialog() 禁用其他表单以防止用户输入当前表单的那些表单。但是大多数其他东西(比如计时器和来自显示表单外部的其他事件)继续运行。