Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

winforms

People also ask

Is a modal window a child window?

A modal window is a child window that requires the user to interact with it before they can return to operating the parent application, thus preventing any work on the application main window. One thing you notice in this definition is that it talks both about windows and applications.

Is modal dialog is blocking window?

A modal window creates a mode that disables the main window but keeps it visible, with the modal window as a child window in front of it. Users must interact with the modal window before they can return to the parent application. This avoids interrupting the workflow on the main window.

What Windows form controls?

Windows Forms controls are reusable components that encapsulate user interface functionality and are used in client-side, Windows-based applications. Not only does Windows Forms provide many ready-to-use controls, it also provides the infrastructure for developing your own controls.


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

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

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

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 is possible to tell if there are any modal forms open:

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

This is an illustration why this answer is correct and the assumptions made in this answer are sometimes wrong.

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

This works for modal shown sub Forms and for MessageBoxes.


Some demo code:

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

Result:

enter image description here