【转】只打开一个子窗口

检查要打开的窗口是否已经是打开状态

 

private void TestApplicationOpenFormsMethod()
{
#region 测试代码一
Form temp_form = new Form();
temp_form.Name = "MyForm";

foreach (Form f in Application.OpenForms)
{
if (f.Name == temp_form.Name)
{
MessageBox.Show("I Found it.");
return;
}
}
MessageBox.Show("I not found it.");//代码二可以执行到这里,说明Application.OpenForms集合是没有这个temp_form的。
return;
#endregion

#region 测试代码二
Form temp_form = new Form();
temp_form.Name = "MyForm";
temp_form.Show();//主要就是这里与测试代码一的不一样。

foreach (Form f in Application.OpenForms)
{
if (f.Name == temp_form.Name)
{
MessageBox.Show("I Found it.");//代码二可以执行到这里,说明Application.OpenForms集合是有这个temp_form的。
return;
}
}
MessageBox.Show("I not found it.");
return;
#endregion

//现在主要我想解决的是。
//我想窗体不想Show()之后Application.OpenForms里有我想要的指定的已经New过的Form;
//是否有相关的Application.属性呢?
//我也不可能用Form.Show()之后,再马上用Form.Hide()吧?这方法帮笨了。
//如果有现成的方法可以在不用Form.Show()后,又可以拿到所有已经new()过的(即在内存的位置的)的窗体的集合。
//我该怎么写呢?
}



posted @ 2011-11-16 17:34  心_远  阅读(222)  评论(0编辑  收藏  举报