Search code examples
c#winformsmodal-dialogmdi

Problem displaying FolderBrowseDialog modally when main form is MDI parent


I have a WinForms application which uses an MDI parent (yes, I know both are out of fashion but this is the required solution).

The MDI parent has a menu option to display a settings form which is displayed modally - this works just fine.

The settings form had a couple of directory paths on it which require setting so I wanted to use the FolderBrowserDialog to permit the user to browse to the desired directories. Pressing the "Browse..." button should show the dialog, also modally, and allow the user to select a folder, however the code simply locks up when I call...

if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK)

The debugger stops, doesn't advance beyond the line, no browser window appears and the entire application becomes totally unresponsive. No exception, no error, just locks-up the entire app.

I have checked the forms properties, changing the TopMost of both modal windows (all permutations). I have confirmed the ownership of the dialog forms by using this when calling the ShowDialog, I've checked for exceptions and checked the call stack.

There is no error raised, no exception that I can see, the code simply stops. I even tried hiding the setting form just prior to display and parenting the dialog off the MDI parent. No joy.

I have also tried using a BackgroundWorker to try to display the dialog on another thread in case the UI thread was blocking (suggested by CoPilot). Didn't work.

What I have ended up doing is creating my own FolderBrowserDialog form and displaying that - works perfectly, displays a modal dialog above a modal window above an MDI parent, but it looks different and means I must either spend ages mimicking the actual dialog or provide a poorer user experience.

I also found attempting to display the FolderBrowserDialog directly from the MDI parent again fails and simply locks up. Trying to parent against the child MDI forms doesn't work either.

Thoughts? Has anyone else come across this before and, more importantly, found a solution?

Thanks


Solution

  • I have just created a minimal test app - an MDIParent form which opens a non-MDI child form modally from a menu option, which then opens the browser dialog from a button on it.

    It works perfectly and the code is essentially the same in my main app, which I've stripped almost everything from to the same point.

    So there is clearly something "corrupt" in my main app. code that I can't identify yet. I have a few more libraries and packages to remove but they are essentially the same.

    Edit: Found it! Turns out it is a very small omission in the definition of the main parent form...

    public static class Program
    {
        [STAThread]
        public static void Main()
    

    Without the [STAThread] the application works perfectly well but the system dialogs can't appear; with it, they work just perfectly.

    The accepted answer here gives a great explanation.