简体   繁体   中英

How to bring a child window to the front in a c# winform application?

When I click on a button in the main form, I want another form to open within the main form. This works so far with the following code:

private void btnOpenChildForm(object sender, EventArgs e)
{       
    ChildForm Form = new ChildForm();
    this.IsMdiContainer = true;
    Form.MdiParent = this;

    Form.Show();
}

Problem:

The buttons and other controls of the main form are still visible in the child form. I tried it with Form.BringToFront() , but it did not work neither.

Update

This is what worked so far for me, after deciding to change the GUI design. I took a ToolStripMenuItem instead of the buttons I had before.

A global variable(I think I have to improve that)

    ChildForm frmChildForm;

The click method:

    private void frmChildFormToolStripMenuItem_Click(object sender, EventArgs e)
            {

                if (frmChildForm  == null || frmChildForm .IsDisposed == true)
                {
                    frmChildForm  = new ChildForm();

                }

                this.IsMdiContainer = true;
                frmChildForm.MdiParent = this;
                frmChildForm.WindowState = FormWindowState.Maximized;
                frmChildForm.Show();

            }   

I Think , you need to use Focus() method for that.

private void btnOpenChildForm(object sender, EventArgs e)
{
    ChildForm Form = new ChildFrom();
    this.IsMdiContainer = true;
    Form.MdiParent = this;
    Form.Show();
    Form.focus();
}

Like i said Ideally the MDI should only have the Menu control and Status Bar and that would be the intention of a MDI. To just act as a container for other forms nothing else.

So if you just want a child form to open up on top of your parentform do this (SDI way).

private void btnOpenChildForm(object sender, EventArgs e)
{   
    //if you just want the form to show on top do not make the MDI

    ChildForm Form = new ChildForm();
    Form.Show();
    Form.Owner=this;    
}

Can you try this from http://www.codeproject.com/Articles/7571/Creating-MDI-application-using-C-Walkthrough :

ChildForm Form = ChildForm.GetChildInstance();
Form.MdiParent = this;
Form.Show();
Form.BringToFront();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM