Switching from MDI to SDI and Back Again
Posted
by Mike Hofer
on Stack Overflow
See other posts from Stack Overflow
or by Mike Hofer
Published on 2009-11-16T22:20:51Z
Indexed on
2010/03/21
19:01 UTC
Read the original article
Hit count: 551
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.
Open MDI child windows don't become SDI windows as I would have expected them to.
Open SDI windows don't become MDI child windows as I would have expected them to.
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!
© Stack Overflow or respective owner