Frage

I have created an MDI application that I want to add a ToolStripContainer to. My first try was to just add one and dock it on Top. Problem I got here was moving ToolStrips inside the container's ToolStripPanels would create an extra Row which was invisible.

Then I saw this question and tried to implement my do-it-yourself MDI interface, using the ToolStripContainer's ContentPanel as where the windows go. This way, I can have my ToolStrips anywhere around the MDI.

The code I have is as follows:

Form f = new Form();
f.TopLevel = false;
tsContainer.ContentPanel.Controls.Add(f);

It all works remarkably well, but I am wondering if this is smart, would I not run into problems later on? Also, if this is possible, why have the entire MDI functionality in Windows?

War es hilfreich?

Lösung

By setting the TopLevel property to false, the form essentially turns into a UserControl. Do note that you have to explicitly set the Visible property to true, it isn't automatic.

But yes, there certainly are problems and it doesn't behave like an MDI child window at all. The biggest issue should be readily visible, the form can no longer be activated. Very noticeable from its caption bar, it will always be rendered with the colors for a non-active window. It however still works like a caption bar, the user can drag the window and move it around. Which has the clear failure mode that the window is going to get clipped without scrollbars. Not only that, the window can still be maximized and minimized by double-clicking the caption. An MDI child does this very differently. And the visual style for the frame will be wrong in Windows 8.x, looking like a Win7 frame instead, a quirk that has no known workaround.

Realistically, you have to set the FormBorderStyle property to None so it behaves more like a true child window. Or write a bunch of fairly nasty code in the WndProc() override to tame the window, you'll be doomed to re-invent an MDI child, imperfectly.

Which is otherwise a fine way to embed a form. The only remaining quirk after fixing the border is that the form still gets added to the Application.OpenForms collection.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top