views:

306

answers:

2

This sounds like it would be a simple task, but I'm running into some issues.

I have some fairly straightforward code for my C# application:

    private void SwitchToSdi()
    {
        MainWindow mainWindow = GetMainWindow();

        for (int index = mainWindow.MdiChildren.Length - 1; index >= 0; index--)
        {
            Form form = mainWindow.MdiChildren[index];
            if (!(form is MainWindow))
            {                    
                form.Visible = false;
                form.MdiParent = null;
                form.Visible = true;
                mainWindow.MdiChildren[index] = null;
            }
        }
        mainWindow.IsMdiContainer = false;
    }

And then,

    private void SwitchToMdi()
    {
        MainWindow mainWindow = GetMainWindow();
        mainWindow.IsMdiContainer = true;
        for (int index = Application.OpenForms.Count - 1; index >= 0; index--)
        {
            Form form = Application.OpenForms[index];
            if (!(form is MainWindow))
            {
                form.Visible = false;
                form.MdiParent = mainWindow;
                form.Visible = true;
            }
        }
    }

Note that MainWindow is an MDI parent window, with its IsMdiContainer property set to True.

The user can toggle between MDI and SDI in the Options dialog. That much works beautifully. If I switch to SDI, the new windows open up OUTSIDE the main window, which is great. Similarly, if I switch to MDI, they open up inside the container.

However, I've noticed a few things.

  1. Open MDI child windows don't become SDI windows as I would have expected them to.

  2. Open SDI windows don't become MDI child windows as I would have expected them to.

  3. Even after I set IsMdiContainer to true in the call to SwitchToMdi(), if I try to open a new window, I get an exception that tells me that the main window isn't an MDI container. o_O

Someone please throw me a bone here. This shouldn't be rocket science. But I'm not finding a whole lot of useful help out there on the Intarwebs (read: g00gle is fairly useless).

Has anyone actually implemented this behavior in .NET before? How did you achieve it? What am I missing here? Halp!

A: 

Here is a lame answer, but you have gone 18 hours without one, so here goes...

In the Borland/Embarcadero C++Builder help, it says "It is not advisable to change FormStyle [between SDI and MDI] at runtime".

Kyle Heironimus
LOL -- You're right. That is a lame answer. :0) But here's the hitch. For this particular *type* of application, there are times when MDI is *extremely* useful, and times when breaking out of MDI is *extremely* useful (multiple monitor setups come to mind). Think of a database explorer.
Mike Hofer
Just out of curiosity, I'd like to know *why* it's not advisable. Is it because users can't handle it, or because of some issue with the tool/OS/whatever? If it's a user issue, I don't think that'll be an issue. My target audience isn't going to be users who can't figure out how to swap between MDI and SDI.
Mike Hofer
@Mike - I dug around quite a bit trying to find out why it was not advisable, because I was having a problem with MDI windows. I never could find a decent answer though. Meanwhile, my problem was because of a dumb omission that had absolutely nothing to do with MDI.
Kyle Heironimus
+1  A: 

Have to say that I didn't do that before, just coming across the same issue. To offer a solution, my current thought is you could mimic the desired behaviour by "copying" the state of the form. Create a new instance and pass the state to a constructor.

Maximilian Becker