Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将mdi子窗体置于前面_C#_Mdi - Fatal编程技术网

C# 无法将mdi子窗体置于前面

C# 无法将mdi子窗体置于前面,c#,mdi,C#,Mdi,我有一个MDI父窗体,我在其中打开MDI子窗体,但如果我再次单击,我不想重新打开它们,而不是我想专注于已打开的特定MDI子窗体,我通过单击菜单条来完成此工作。我已经做了很多尝试来成功地做到这一点,但总是失败。我的代码是: 这是一种方法 private bool CheckMdiClientDuplicates(string WndCls) { Form[] mdichld = this.MdiChildren; if (this.MdiChildren.Length == 0)

我有一个MDI父窗体,我在其中打开MDI子窗体,但如果我再次单击,我不想重新打开它们,而不是我想专注于已打开的特定MDI子窗体,我通过单击菜单条来完成此工作。我已经做了很多尝试来成功地做到这一点,但总是失败。我的代码是:

这是一种方法

private bool CheckMdiClientDuplicates(string WndCls)
{
    Form[] mdichld = this.MdiChildren;
    if (this.MdiChildren.Length == 0)
    {
        return true;
    }
    foreach (Form selfm in mdichld)
    {
        string str = selfm.Name;
        str = str.IndexOf(WndCls).ToString();
        if (str != "-1")
        {
            return true;
        }
    }
    return false;
}
我正在通过

private void myToolStripMenuItem_Click(object sender, EventArgs e)
{
    MyForm f = new MyForm();//MyForm is the form on which i am working
    if (CheckMdiClientDuplicates("MyNamespace.MyForm") == true)
    {
        f.MdiParent = this;
        f.Show();
    }
    else
    {
        f.Activate();
        f.BringToFront();
    }
}

f
仍然是您的
新MyForm()
,那么您希望
激活它能做什么?你需要得到你想要带到前面的实际表单,然后
激活它


此外,您可能不想创建新的
MyForm
,除非您打算使用它。

您应该尝试以下代码:

当您单击按钮以显示第一个表单时:

private void button1_click(object sender, EventArgs e){
        Form1 f1 = null;
        if (IsFormAlreadyOpen(typeof(Form1)) == null)
        {
            if (ActiveMdiChild != null)
            {
                ActiveMdiChild.Close();
            }
            f1 = new Form1();
            f1.MdiParent = this;
            f1.Show();
        }
}
方法:

public static Form IsFormAlreadyOpen(Type FormType)
    {
        foreach (Form OpenForm in Application.OpenForms)
        {
            if (OpenForm.GetType() == FormType)
                return OpenForm;
        }

        return null;
    }

单击其他表单的“下一步”按钮时,请执行相同操作。

@Sandy.Net查看您的代码。您正在使用
MyForm f=new MyForm()
创建一个新表单,然后尝试执行
f.Activate()
…那么在这种情况下,先生,我应该怎么做?我应该先关闭from,然后用新实例重新打开它吗?@Sandy.Net您应该在现有MyForm对象上调用
Activate()
,而不是创建一个新实例。最好的解决方案可能是从
CheckMdiClientDuplicates
返回
MyForm
,而不是
bool