1

概述:

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

描述:

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

问题:

一个。单击搜索。然后单击主页。

湾。如果再次单击“搜索”,它不再将其对应的、已打开的表单带到前面。

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

3 回答 3

3

tsmHome_Click您的代码对我有用..在您的事件处理程序 中更改了一行之后

你有过。

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

它应该是。

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

看起来像复制粘贴错误让你。

于 2014-12-12T04:57:41.797 回答
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();
于 2014-12-12T04:07:38.343 回答
0

您可以尝试几种选择:

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

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

f.ShowDialog();

希望这会有所帮助。此致,

于 2014-12-12T03:54:34.060 回答