如何用C#计算所有定义的子窗体

本文关键字:窗体 定义 何用 计算所 | 更新日期: 2023-09-27 18:28:50

我尝试使用Count()或Length来计算打开了多少Can_ListCandidate childfrom。然后,如果只有一个表单运行,它仍然保持。而如果数字超过2,它们都将关闭。但是,f.GetType() == typeof(Can_ListCandidate)没有返回任何数字的选项。

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

如何用C#计算所有定义的子窗体

我不清楚你想做什么,但如果你想用谓词计数,你可以使用:

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

包括子类或

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

但事实并非如此。

当然,这两个都不会处理表格。。。