How to unload all open forms in VB.NET?

In the middle of converting VB6 code to VB.NET, I need to replace the following code, which intends to close all open forms left in the application.

'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 replaced the function Unloadwith Close(as indicated in TFM), but the compiler complains that OpenFormsit is not a member My.Application.

Where can I open open forms?

+3
source share
5 answers

The property OpenFormsreturns a FormCollection. You can iterate through the collection to process all forms.

For each f as Form in My.Application.OpenForms
 f.Close()
Next
+11
source

I found 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

... ( verbose), , .

+7

Application.Exit .

AS , , .

+5

My.Application.OpenForms , VB.Net (. " , , " ).

, Application.OpenForms( System.Windows.Forms).

+1

Source: https://habr.com/ru/post/1703876/


All Articles