表格可以判断是否有任何模态窗口打开?

时间:2009-01-03 21:57:57

标签: c# .net winforms

如何,在我的WinForm应用程序的主要形式内,我可以判断是否有任何模态窗口/对话框打开属于主窗体?

6 个答案:

答案 0 :(得分:31)

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

答案 1 :(得分:6)

长话短说:只要模态窗口打开,打开一个模态窗体就是主窗体上的块执行,所以你的主窗体永远不能检查它是否打开任何模态窗体直到模态窗体之后已关闭。换句话说,你的问题是基于对模态形式如何运作的误解,所以它完全没有实际意义。

为了它的价值, 可以判断是否有任何模态表格打开:

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

答案 2 :(得分:5)

您可以使用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; }
    }
}

答案 3 :(得分:1)

这说明了this answer是正确的,而this answer中所作的假设有时是错误的。

if (MyForm.CanFocus == false && MyForm.Visible == true)
{
    // we are modal            
}

这适用于模式显示的子窗体和MessageBoxes。


一些演示代码:

private void modalTest_Click(object sender, EventArgs e)
{
    // Timer which fires 100ms after the first message box is shown
    System.Windows.Forms.Timer canFocusTimer = new System.Windows.Forms.Timer();
    canFocusTimer.Tick += CanFocusTimer_Tick;
    canFocusTimer.Interval = 100;
    canFocusTimer.Start();
    
    // First MessageBox shows that CanFocus == true
    MessageBox.Show($"First MessageBox: { nameof(this.CanFocus) } == { this.CanFocus }");
}

private void CanFocusTimer_Tick(object sender, EventArgs e)
{
    (sender as System.Windows.Forms.Timer).Stop();

    // Even though there is already a MessageBox shown the second gets
    // displayed. But now CanFocus == false
    MessageBox.Show($"Second MessageBox: { nameof(this.CanFocus) } == { this.CanFocus }");
}

结果:

enter image description here

答案 4 :(得分:0)

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

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");
    }
}

答案 5 :(得分:0)

如果你有点Google,你会发现Form.ShowDialog()禁用其他表单以阻止用户输入当前表单的那些形式。但是大多数其他东西(如计时器和显示形式外部来源的其他事件)继续运行。