5

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

我尝试了以下方法,测试了一些代码,但它一直说表单没有打开,即使我知道它是:

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

3 回答 3

20

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

if (Application.OpenForms.OfType<YouLikeHits_Settings>().Any())
    MessageBox.Show("Form is opened");
else
    MessageBox.Show("Form is not opened");
于 2012-11-18T22:13:10.417 回答
1

这肯定会奏效

            if (Application.OpenForms.OfType<frm_YouLikeHits_Settings>().Any())
            {
                Application.OpenForms.OfType<frm_YouLikeHits_Settings>().First().Close();
            }
            frm_YouLikeHits_Settings f1= new frm_YouLikeHits_Settings();
            f1.MdiParent = this;
            f1.Show();
于 2016-06-30T06:00:19.153 回答
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)
{

}
于 2018-05-26T01:15:22.190 回答