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

标签 c# mdichild mdiparent topmost bringtofront

概述:

我有一个 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();
    }

最佳答案

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

你有。

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

应该是。

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

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

关于c# - 如何将 MDI 子窗体带到前面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27436531/

相关文章:

c# - WPF:绑定(bind)到 MainWindow 属性

c# - 如何在 C# 中的一个连接字符串中连接到两个数据库?

c# - MDIchild 窗体位于面板后面

c# - 我如何知道新的 MDI 表单已添加到父 MDI 表单中?

c# - MDI Form 有焦点吗?

java - JAVA MDI 应用程序

c# - 我可以在 ConcurrentBag 上使用普通的 foreach 吗?

c# - 需要有关 Enumerable.Aggregate 函数的更多详细信息

C# MDI Parent 检测 MDI Child 何时关闭?

c# - MDI 窗体不能改变光标