繁体   English   中英

检查MdiChildren是否存在,c#

[英]to check if MdiChildren exists ,c#

我有MDI应用程序,并且在焦点/选择上采用了新形式。 为了避免多次打开同一张图片,我编写了这段代码,但这有一个问题

私人void lstview1_MouseDoubleClick(object sender,MouseEventArgs e){
字符串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);

            }

}

子形式标记再次是int.parse(window_name)。 但是它会引发错误,从而使[sensce [this.MdiChildren [index] .Tag]首先需要存在。 我该如何改善这种存在性,或者如何使我的代码更好。

那么这种方法呢:

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

暂无
暂无

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

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