2011-01-05 76 views
0

我有MDI應用程序,並在焦點/選擇上開啓新窗體。到avoide相同的圖像的開口不止一次我寫這一段代碼,但它有一個問題檢查是否存在MdiChildren,c#

私人無效lstview1_MouseDoubleClick(對象發件人,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); 

      } 

}

其中childform標籤再次int.parse(window_name)。 但它會拋出錯誤,這使得sensce [this.MdiChildren [index] .Tag]需要先存在。 我該如何克服這種存在/或如何讓我的代碼更好。

回答

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

感謝名單,有一些小的變化,它的工作原理 – Shahgee 2011-01-06 06:36:46