2014-10-31 14 views
0

我試圖使用Count()或Length來計算有多少Can_ListCandidate childfrom正在打開。那麼,如果只有一個表單運行,它仍然保持。而如果這個數字超過2,他們都將被關閉。但是,f.GetType() == typeof(Can_ListCandidate)無法返回任何號碼。如何使用C#對所有定義的子表單進行計數

foreach(Form f in this.MdiChildren) { 
    if (f.GetType() == typeof(Can_ListCandidate)) { 
    f.Dispose(); 
    } 
} 

回答

0

這有點我不清楚你想要做什麼,但如果你想用謂詞來算,你可以使用:

int count = MdiChildren.OfType<Can_ListCandidate>().Count(); 

,其中包括子類或

int count = MdiChildren.Count(x => x.GetType() == typeof(Can_ListCandidate)); 

哪個沒有。

這些都不會處理表格,當然...

+0

謝謝你的評論。我想要數'Can_ListCandidate'形式,並且可以將您的支持腳本放到我的代碼中? – 2014-10-31 18:51:03

+1

@ PMay1903:「支持腳本」是什麼意思?該代碼會計算'MdiChildren',它們是'Can_ListCandidate'或者子類,或者''Can_ListCandidate'。你可以在任何你有權訪問'MdiChildren'的地方使用它......你現在的代碼在哪裏? – 2014-10-31 18:53:01

相關問題