1

I have a MDI form with many controls inside it like button,images,label,dropdown etc.I have made the form property isMDIContainer=true.When i click on a button inside the form another form is to be opened inside the parent.Now its opening,but unfortunately its opening behind all the controls.How to make the child form open infront of all the controls of main form?

    Form2 childForm = new Form2();            
    childForm.MdiParent = this;
    childForm.Activate();          
    childForm.Show();
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
Mothy
  • 406
  • 1
  • 7
  • 19
  • What you're asking, as far as I'm aware, isn't both possible and practical. Although you can put controls on an MDI container, that's mainly there to enable the use of Toolbars and StatusBars. MDIChildren always show up under the main elements of the form. – Adrian Aug 26 '13 at 02:14
  • If the OP chose to go with the method described in the answer to that question, I'd suggest using the Form.Closing event to inform the parents form that the child is closing so it can evaluate if it's going to make the menu panel appear or disappear, rather than wasting CPU cycles on a timer when 99 cycles out of 100 it's going to do nothing with it. – Adrian Aug 26 '13 at 02:32
  • @Adrian The accepted answer of that question is not necessarily the one I would go with. There are other good ideas in the other answers and links in that question. One of them should work for the OP – Mark Hall Aug 26 '13 at 02:42
  • Were this my project I'd advocate using a Toolbar instead of controls on the MdiForm. – Adrian Aug 26 '13 at 04:21
  • @MarkHall : Could you please help me to find an answer? – Mothy Aug 26 '13 at 05:00

2 Answers2

3

Normally we don't add any child controls to a Mdi Form. When a Form is used as an Mdi Form, the only child it should contain is MdiClient. That MdiClient will contain your child forms. All the controls should be placed on the Child forms. However if you want so, we can still make it work

There is a default MdiClient contained in a Mdi Form. We can find it in the Controls collection of the Mdi Form. It's type of MdiClient. This will be covered by all other controls of your Mdi Form and that's why your Child forms can't be brought on top by default. To solve it, simply we have to access to the MdiClient and call BringToFont() and whenever there isn't any Child form being Visible, we will call SendToBack() on the MdiClient to show the other controls of yours (button,images,label,dropdown etc). Here is the code for you to test:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        IsMdiContainer = true;
        //Find the MdiClient and hold it by a variable
        client = Controls.OfType<MdiClient>().First();
        //This will check whenever client gets focused and there aren't any
        //child forms opened, Send the client to back so that the other controls can be shown back.
        client.GotFocus += (s, e) => {
            if (!MdiChildren.Any(x => x.Visible)) client.SendToBack();
        };
    }
    MdiClient client;
    //This is used to show a child form
    //Note that we have to call client.BringToFront();
    private void ShowForm(Form childForm)
    {
        client.BringToFront();//This will make your child form shown on top.
        childForm.Show();            
    }
    //button1 is a button on your Form1 (Mdi container)
    //clicking it to show a child form and see it in action
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f = new Form2 { MdiParent = this };
        ShowForm(f);         
    }     
}
King King
  • 61,710
  • 16
  • 105
  • 130
  • Really good solution, with one caveat: if the user minimises the form instead of closing it, the lambda `GotFocus` event handler isn't fired, so the `MdiClient` doesn't go to the back again. Now this isn't necessarily a bad thing, since if it did that you wouldn't be able to see the minimised form, but it is something to be aware of. – Adrian Aug 26 '13 at 04:20
  • @Adrian when the child form is minimized, the `MdiClient` should still be on top, it will be sent to back only when all the child forms are hidden/closed. That's by design. – King King Aug 26 '13 at 04:24
  • as I said, not necessarily a bad thing. I'm just thinking of it from the perspective of someone not totally familiar with how MDI works and what their expectations for use might be. – Adrian Aug 26 '13 at 04:26
  • @KingKing : Will try your solution and tell you the results – Mothy Aug 26 '13 at 05:01
  • 1
    @KingKing : Thank you for the above solution.I am able to bring the form2 in front of form1 with the help of your code.But all the controls in the form1 is getting invisible when the form2 is opened.I want all the controls in the form1 to be visible too.Can you please help me? – Mothy Aug 26 '13 at 16:11
  • This is not a solution to the problem proposed. I don't know why people answers different things than those that have been asked –  Dec 24 '19 at 12:46
-1

Handle the Shown event and call this.BringToFront();.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • That still doesn't bring the child form in front of the controls specified on MDIContainer. The problem being that the controls float in front of the Panel that forms the area of the MDIContainer that shows the MDIChild forms. – Adrian Aug 26 '13 at 02:21