如何将MDI儿童表格带到前面?

时间:2014-12-12 03:48:27

标签: c# mdichild mdiparent topmost bringtofront

概述:

我有一个MDI父表单,我在其中加载其他表单。加载第二张表格后,我再也无法将第一张表格带到前面。

说明

在父表单上,我有一个包含2个菜单项的菜单条;家庭和搜索。 每个点击事件都会加载相应的表单,除非已经加载了所述表单。

问题:

a。点击“搜索”。然后单击Home。

b。如果再次点击“搜索”,它就不会再将相应的已打开的表单带到前面。

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

3 个答案:

答案 0 :(得分:3)

您的代码正在为我工​​作..在tsmHome_Click事件处理程序

中更改一行后

你有。

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

应该是。

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

看起来像是一个复制粘贴错误。

答案 1 :(得分:0)

您可以尝试以下几种选择:

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

此外,您可以在对话框模式下打开窗口:

f.ShowDialog();

希望这会有所帮助。最好的问候,

答案 2 :(得分:0)

您可以将代码更改为此代码,如果表单存在,请将其置于最前面。

   // 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();