迭代Windows窗体并关闭它们? [英] Iterate through Windows forms and close them?
问题描述
我试图使用下面的代码迭代我的应用程序中的所有当前打开的表单,并关闭它们,除了主表单作为清理的一部分。
Dim openForms As Windows.Forms.FormCollection = Application.OpenForms
For Each frm As Windows。 Forms.Form在openForms
中如果frm.Name.ToString()<> FrmMainNewThen
frm.Close()
End If
Next
但是,我得到一个 InvalidOperationException
,因为执行 frm.Close()
时,是在 openForms
被删除,改变集合的大小。我显然做错了,所以如果任何人都可以在这里指出我的问题,那就太棒了。否则,有没有另外一种方法来做这样的事情?
For ix As Integer = Application.OpenForms.Count - 1 To 0 Step -1
Dim frm = Application.OpenForms(ix)
''等等。
下一个
I'm trying to use the following code to iterate over all of the currently open forms in my application and close them except for the main form as part of a clean up.
Dim openForms As Windows.Forms.FormCollection = Application.OpenForms
For Each frm As Windows.Forms.Form In openForms
If frm.Name.ToString() <> "FrmMainNew" Then
frm.Close()
End If
Next
However, I'm getting an InvalidOperationException
because when frm.Close()
is executed, the entry that was in openForms
is deleted, changing the size of the collection. I'm obviously doing something wrong, so if anyone can point me at the problem here, that would be awesome. Otherwise, is there another way to do something like this?
Iterate backwards so that modifying the collection doesn't byte:
For ix As Integer = Application.OpenForms.Count - 1 To 0 Step -1
Dim frm = Application.OpenForms(ix)
'' etc..
Next
这篇关于迭代Windows窗体并关闭它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!