如果打开关闭表格,如何检查表格是否打开?

如何检查表单是否已打开,以及表单是否打开以关闭表单?

我尝试了下面的代码,测试了一些代码,但它仍然说即使我知道这个代码是不正常的:

foreach(Form a in Application.OpenForms) { if (a is YouLikeHits_Settings) { // About form is open MessageBox.Show("form open"); break; } // About form is not open... MessageBox.Show("form not open"); break; } 

Application.OpenForms包含打开的表单。 如果此集合中有表单,则会打开它。 否则它没有打开(可能关闭)。

 if (Application.OpenForms.OfType().Any()) MessageBox.Show("Form is opened"); else MessageBox.Show("Form is not opened"); 

这将确定

  if (Application.OpenForms.OfType().Any()) { Application.OpenForms.OfType().First().Close(); } frm_YouLikeHits_Settings f1= new frm_YouLikeHits_Settings(); f1.MdiParent = this; f1.Show(); 
 try { if (Application.OpenForms.OfType().Any()) { talkForm frm = new talkForm(); frm.Close(); MessageBox.Show("Form is opened"); } else { talkForm frm = new talkForm(); frm.Show(); MessageBox.Show("Form is not opened"); } } catch(Exception ex) { }