Switching from MDI to SDI and Back Again
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,
开发者_如何学C 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!
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.
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".
精彩评论