如何卸载 VB.NET 中所有打开的表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/589503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I unload all open forms in VB.NET?
提问by brasskazoo
In the middle of converting VB6 code to VB.NET, I need to replace the following code that intends to close all open forms remaining in the application.
在将 VB6 代码转换为 VB.NET 的过程中,我需要替换以下旨在关闭应用程序中剩余的所有打开表单的代码。
'close all sub forms
For i = My.Application.OpenForms.Count - 1 To 1 Step -1
'UPGRADE_ISSUE: Unload Forms() was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="875EBAD7-D704-4539-9969-BC7DBDAA62A2"'
Unload(My.Application.OpenForms(i))
Next i
I've replaced the Unload
function with Close
(as indicated by TFM), but the compiler complains that OpenForms
is not a member of My.Application
.
我已经Unload
用Close
(如TFM 所示)替换了该函数,但编译器抱怨它OpenForms
不是My.Application
.
Where can I access the open forms?
我在哪里可以访问打开的表格?
回答by Cerebrus
The OpenForms
property returns a FormCollection
. You can iterate through the collection to process all forms.
该OpenForms
属性返回一个FormCollection
。您可以遍历集合以处理所有表单。
For each f as Form in My.Application.OpenForms
f.Close()
Next
回答by brasskazoo
I uncovered this solution,
我发现了这个解决方案,
'close all sub forms
For i = System.Windows.Forms.Application.OpenForms.Count - 1 To 1 Step -1
Dim form As Form = System.Windows.Forms.Application.OpenForms(i)
form.Close()
Next i
...which looks alright (if not verbose), and I'll be able to test it just as soon as I can compile everything else..
...看起来不错(如果不是冗长的),我将能够在编译其他所有内容后立即对其进行测试..
回答by chrissie1
Application.Exit will pretty much do the same.
Application.Exit 几乎会做同样的事情。
AS I suppose you want to close the application anyway if all forms are closed.
因为我想如果所有表单都关闭,您无论如何都想关闭应用程序。
回答by Nick Wallace
The My.Application.OpenForms requires the VB.Net application to use the Application Framework (see Project Properties, Application, Enable Application Framework).
My.Application.OpenForms 要求 VB.Net 应用程序使用应用程序框架(请参阅项目属性、应用程序、启用应用程序框架)。
If you don't user the Application Framework, you can instead use the Application.OpenForms (from System.Windows.Forms namespace).
如果您不使用应用程序框架,则可以改用 Application.OpenForms(来自 System.Windows.Forms 命名空间)。
回答by Franci Penov
Have a look at the Application.Windows
property.
看一看Application.Windows
楼盘。