【问题标题】:to check if MdiChildren exists ,c#检查 MdiChildren 是否存在,c#
【发布时间】:2011-01-06 05:11:54
【问题描述】:

我有 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)。 但它会引发错误,这使得 sensce [ this.MdiChildren[index].Tag] 需要首先存在。 我怎样才能检查这个存在/或者我怎样才能让我的代码更好。

【问题讨论】:

    标签: c#


    【解决方案1】:

    这种方法怎么样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 2013-05-19
      • 2011-10-09
      • 2011-08-31
      • 1970-01-01
      相关资源
      最近更新 更多