Controls in Container Form Come Over Child Form

Controls in container form come over child form?

I've also got the same problem. I got an alternative solution as described below:

  1. Insert a timer control
  2. I've added the controls in a panel container
  3. And did the following

    private void timer1_Tick(object sender, EventArgs e)
    {
    if ((int)MdiChildren.GetLength(0) > 0)
    {
    panel1.Visible = false;
    }
    else
    {
    panel1.Visible = true;
    }
    }

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

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.

Child form opened behind the controls of the main form in c#

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);
}
}

Access child form group control elements from mdi parent

quick and dirty method :

create a method on each MDI Child form like this :

// suppose this mdi child is called FormMDIChild_1

public GroupBox GetGroupBox()
{
return Groupbox1;
}

in MDI Parent do this :

if (ActiveMdiChild is FormMDIChild_1)
{
GroupBox myGroupBox = ((FormMDIChild_1)ActiveMdiChild).GetGroupBox();
}

Better solution :

Create an MDI Child and call it for example FormBaseMDIChild

On this FormBaseMDIChild create a virtual method

public virtual GroupBox GetGroupBox()

inherit all other MDI Childs forms from FormBaseMDICHild and override the method GetGroupBox()

In MDI Parent do this

myGroupBox = ((FormBaseMDICHild)ActiveMdiChild).GetGroupBox();


Related Topics



Leave a reply



Submit