【问题标题】:How to bring MDI Child Form To Front?如何将 MDI 子表单放在前面?
【发布时间】:2014-12-12 03:48:26
【问题描述】:

概述:

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

说明:

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

问题:

a. 点击搜索。然后点击主页。

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

【问题讨论】:

  • 看来,如果我注释掉 //return;它按预期工作,但是,它创建了相同类型的新形式。想法?
  • 格兰特,无论哪个。我只想让用户点击一个菜单项,并将其对应的已经打开的表单带到前面。
  • 我只是想说我讨厌你的名字。就这些。 ;)

标签: c# mdichild mdiparent topmost bringtofront


【解决方案1】:

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

你有。

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

应该是的。

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

您似乎遇到了复制粘贴错误。

【讨论】:

  • 第二双眼睛可以提供帮助。 :)
【解决方案2】:

您可以尝试多种选择:

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

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

f.ShowDialog();

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多