如何关闭动态创建的所有已打开表单?
Sub OpenForms
dim F as new frmForm1
F.show
dim F2 as new frmForm1
F2.show
End Sub
sub CloseForms
'doesn't work, (forms are created in a dll and ".openforms" method is not available):
'For each f as Form in My.Application.OpenForms
' f.Close()
'Next
end Sub
我想关闭所有打开的frmForm1 - 任何想法?
答案 0 :(得分:1)
来自MSDN:
My.Application.OpenForms属性返回所有打开的表单,无论哪个线程打开它们。您应该在访问之前检查每个表单的InvokeRequired属性;否则,它可能会抛出InvalidOperationException异常。有关详细信息,请参阅How to: Access All Open Forms of an Application (Visual Basic)
希望有所帮助。
答案 1 :(得分:0)
只需创建一个列表/集合并将所有创建的表单添加到其中。然后你可以遍历列表/集合来关闭所有。以下是C#中的示例:
List<frmForm1> _forms = new List<frmForm1>();
void OpenForms()
{
var f1 = new frmForm1();
_forms.Add(f1);
var f2 = new frmForm1();
_forms.Add(f2);
}
void CloseForms()
{
foreach (var form in _forms)
{
form.Close();
}
}