简体   繁体   中英

How to bring MDI Child Form To Front?

Overview:

I have an MDI Parent Form in which I load other forms. After loading the second form, I can no longer bring the first one to the front.

Description:

On the parent form I have a menu strip containing 2 menu items; Home and Search. Each click event loads their corresponding form unless said form is already loaded.

The problem:

a. Click Search. Then click Home.

b. If Search is once again clicked, it no longer brings its corresponding, already opened form to the front.

    private void tsmHome_Click(object sender, EventArgs e)
    {
        // Loop through all open forms...
        foreach (Form form in Application.OpenForms)
        {
            // If frmHome is Opened, set focus to it and exit subroutine.
            if (form.GetType() == typeof(frmSearch))
            {

                form.Activate();
                return;
            }
        }

        // If frmHome is not Opened, create it. 
        frmHome f = new frmHome();
        f.MdiParent = this;
        f.Show();
    }

    private void tsmSearch_Click(object sender, EventArgs e)
    {
        // Loop through all open forms...
        foreach (Form form in Application.OpenForms)
        {
            // If frmSearch is Opened, set focus to it and exit subroutine.
            if (form.GetType() == typeof(frmSearch))
            {

                form.Activate();
                return;
            }
        }

        // If frmSearch is not Opened, create it. 
        frmSearch f = new frmSearch();
        f.MdiParent = this;
        f.Show();
    }

Your code is working for me.. After changing one line in your tsmHome_Click event handler

You had.

if (form.GetType() == typeof(frmSearch))

It should be.

if (form.GetType() == typeof(frmHome))

Looks like a copy paste error got you.

You can try several options:

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

Also, you can open the window in Dialog mode:

f.ShowDialog();

Hope this will help. Best regards,

You can change code to this, if form exist make it bring to front.

   // Loop through all open forms...
    foreach (Form form in Application.OpenForms)
    {
        // If frmSearch is Opened, set focus to it and exit subroutine.
        if (form.GetType() == typeof(frmSearch))
        {

            form.Activate();
            form.BringToFront();
            //form.WindowState = FormWindowState.Maximized;
            return;
        }
    }

    // If frmSearch is not Opened, create it. 
    frmSearch f = new frmSearch();
    f.MdiParent = this;
    f.Show();
foreach (Form form in System.Windows.Forms.Application.OpenForms)
{                
    if (form.GetType() == typeof(Tviewer))
    {       
        form.WindowState = FormWindowState.Minimized;                     
        form.WindowState = FormWindowState.Normal;
        return;
    }
}

//It worked for me.

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