检查MdiChildren是否存在,c#

时间:2011-01-05 20:53:59

标签: c#

我有MDI应用程序并在焦点/选择上打开新表单。为了避免多次打开相同的图像,我已经编写了这段代码,但它有一个问题

private void lstview1_MouseDoubleClick(object sender,MouseEventArgs e)         {
                string window_name = this.lstview1.FocusedItem.Tag.ToString();

            if (this.MdiChildren.Count() > 0)
            {

                if ( window_name == this.MdiChildren[i].Tag.ToString()) // At this point  need ur help
                {
                    this.MdiChildren[i].Activate();
                }
                else
                {
                    Image_show_form(image, window_name);

                }
            }
            else
            {
                Image_show_form(image, window_name);

            }

}

其中childform标记又是int.parse(window_name)。 但它抛出的错误使得[this.MdiChildren [index] .Tag]首先需要存在。 我怎样才能解决这个问题?或者如何让我的代码更好。

1 个答案:

答案 0 :(得分:0)

这种方法怎么样:

private void ShowForm(string name)
{
    Form targetForm = null;

    foreach (Form frm in Application.OpenForms)
    {
        if (frm.Tag != null)
        {
            if (frm.Tag.ToString() == name)
            {
                targetForm = frm;
                break;
            }
        }
    }

    if (targetForm != null)
    {
        targetForm.Activate();
    }
    else
    {
        // create new form and show it
    }
}
相关问题