简体   繁体   English

如何将 MDI 子窗体带到前面?

[英]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.我有一个MDI Parent Form,其中我加载了其他forms。加载第二个表格后,我不能再将第一个放在前面。

Description:描述:

On the parent form I have a menu strip containing 2 menu items;在父窗体上,我有一个包含 2 个菜单项的菜单条; 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. 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 您的代码正在为我工​​作..在您的tsmHome_Click事件处理程序中更改一行后

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. //它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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