samtal
samtal

Reputation: 131

Unable to display Child form on top of main form's controls

Windows 7 pro 64 bit, VS2015 or VS2019
Hi,
I have a C# Win Form with many various controls.
I defined the main form as MDI parent and built an MDI child form with it's own controls, activated by a menue item in the main.
The child form builds nicely, but it is always displayed under the main form's many different controls.
I have tried many remedies, non of whch solved the problem. I'v Set the child form as TopMost = true; TopLevel = true; each or all, for no avail. Have moved from VS2015 to VS2019 community - Same. I have been wasting hours to solve something that seems to be strightforward.
Can anyone help me out of this?

//In Main Form with menustrip, ComPortSetup is a standard winform class with some controls  
private void portSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ComPortSetup comPortSetup = new ComPortSetup();
            comPortSetup.MdiParent = this;
            comPortSetup.TopMost = true;
            comPortSetup.TopLevel = true; //Can not change programmatically
            comPortSetup.Show();
        }

Upvotes: 0

Views: 1615

Answers (2)

GuidoG
GuidoG

Reputation: 12059

That is how MDI works, there is no way around that.
All controls on the MDI Parent form will take away "client" space for the MDI child forms. And thus they will always be shown on top of any MDI Child form.

In other words, the MDI child forms can only use the space that is on the MDI Parent form that is not occupied already by other controls.

What you can do is put a panel on the MDI Parent form, and for example dock it to the left. Then put your "main" controls on that panel. The MDI Child forms will use whatever space is left on your MDI Parent form.
You could put a splitter control next to this panel so you can make it larger or smaller, or make it slidable so the panel comes forward when your mouse is near it, and hides itself again when the mouse is moving away from it.

Another approach you can try, is not making it MDI anymore and set the parent of the "child" forms yourself. But this will most likely cause other problems.

I would try the first approach, the panel on the mainform, docked to the left with a splitter control next to it.

Upvotes: 2

jason.kaisersmith
jason.kaisersmith

Reputation: 9650

I am not sure, but I believe that when using MDI forms, it is not expected that the parent has its own controls on the main form area, otherwise you will experience this exact problem.

So there are a couple of ways around this.

Firstly you can place a Panel on your parent form, and then your child can be added to the Panel.

This is now "proper" MDI control however, but it might allow you to achieve what you want.

ChildForm child = new ChildForm();
parentPanel.controls.add(child);  //ParentPanel needs to already be on main form

Or the other method is to put your Parent Controls either on a MenuStrip (like MS Word) or you can use a Floating Dockable child form (think Visual Studio) which is then always visible.

If you want to do the latter, then I would suggest DockPanelSuite control to help you with this

https://github.com/dockpanelsuite/dockpanelsuite

Upvotes: 1

Related Questions