Search code examples
c#mdichildmdiparent

Show top most Child Form c#


How can I show topmost a button clicked child form in my parent form (with a tabcontrol which docked as fill)? It always shows the form at the back of the tabcontrol of the parent form, I've even used:

frm.TopMost = true;
frm.BringToFront();

Still shows at the back.


Solution

  • What you want is not possible. MDI children of a control get shown on a control (which you can't directly select) called MdiClient, which is not transparent (and can't be) and by default, goes always to the back of other controls in the parent form.

    So the only way to do this, would be getting the MdiClient over the controls in the parent form: this would do what you expect, but it would also hide the parent controls when there are no child forms displayed (since again, the MdiClient is not, and can't be transparent).

    So the only reasonable way would be having a maximized child form with the TabControl, instead of having that TabControl directly on the parent.

    Or you could have your TabControl only shown when there are no child windows. For that, make a timer in the parent form, and check this at every interval:

    if(MdiChildren.Length > 0)
       myTabControl.SendToBack();
    else
       myTabControl.SendToFront();
    

    This will only work if the MDI children are always maximized: your TabControl will not be visible when there are any children (no matter if they cover it or not)

    Update

    As remarked in the comments, you can have "your own MDI", by having a host control (a Panel, for example) in the parent form and loading the child forms in that control:

    var form = new ChildForm();
    form.TopLevel = false;
    form.Parent = myHostPanel;
    form.Show();
    

    This would show the form inside the panel (which you can locate and zorder where you want)... you lose all the MDI management though, and you'll have to keep track of your children (and take care of the forms' events if needed) yourself.

    I'd not use this solution, it's pretty hacky and can get messy for big applications (unless you do a correct system)

    As a summary

    Since we're discussing these methods in the comments

    You can hack your way to do what you want, but you'll come into all sorts of problems with any approach.

    If I was you, I'd redesign my application so that what you want to achieve is not needed. If you can't do that, the only sane way would be just not having those controls in the parent form, have an always-maximized, non-closable MDI child form with those controls, and skip that window everytime you need to work in the MDI children collection.