简体   繁体   English

无法将mdi儿童形式带到前面

[英]unable to bring mdi child form in front

I have a MDI Parent form in which I open MDI Child forms, but I don't want to reopen them if i click another time, instead of that i want to focus on that particular mdi Child form which is already opened, and i am doing this job from clicking my menu strip. 我有一个MDI Parent表单,我在其中打开MDI Child表单,但我不想重新打开它们,如果我点击另一次,而不是我想专注于已经打开的特定mdi子表单,我是通过单击我的菜单条来完成这项工作。 i already tried a lot to do this successfully, but got failed all the time. 我已经成功地尝试了很多,但总是失败了。 well my code is : 我的代码是:

this is a Method... 这是一个方法......

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

and i am implementing this method via... 我正在通过......实施这个方法

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 is still your new MyForm() , so what do you expect Activate ing it will do? f仍然是你的new MyForm() ,那么你期望Activate它会做什么? You need to get the actual form you want to bring to the front and Activate that. 您需要获得想要带到前面的实际表单并Activate它。

Additionally, you probably don't want to make a new MyForm unless you intend to use it. 此外,除非您打算使用它,否则您可能不想创建新的MyForm

You should try this code: 你应该试试这个代码:

This is when you click a button for your first form to appear: 这是当您单击第一个表单的按钮时:

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

Method: 方法:

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

        return null;
    }

Do the same when you click the next button for the other form. 单击其他表单的下一个按钮时,请执行相同操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM