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

时间:2012-11-18 22:11:09

标签: c# winforms form-control

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

我尝试了下面的代码,测试了一些代码,但它仍然说即使我知道它的形式也没有打开:

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

3 个答案:

答案 0 :(得分:15)

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

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

答案 1 :(得分:1)

这将确定

setup pod

答案 2 :(得分:0)

try
{
    if (Application.OpenForms.OfType<talkForm>().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)
{

}