tags:

views:

339

answers:

2

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?

+6  A: 

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
    }
}
Juliet
This is not actually true. If it were true then whenever you moved a File Open dialog accross the owning form it would fail to repaint. So at the least painting will run. Once painting is running you can do anything. There are several windows messages which get through and allow for code execution
JaredPar
Agree with JaredPar, this is a sensible question. E.g., what if you were checking on a different thread, prior to Invoking to show a new dialog?Still, the code sample looks good.
John
A: 

if (this.Visible && !this.CanFocus)