Question

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.

'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.

Where can I access the open forms?

Was it helpful?

Solution

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

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

OTHER TIPS

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..

Application.Exit will pretty much do the same.

AS I suppose you want to close the application anyway if all forms are closed.

The My.Application.OpenForms requires the VB.Net application to use the Application Framework (see Project Properties, Application, Enable Application Framework).

If you don't user the Application Framework, you can instead use the Application.OpenForms (from System.Windows.Forms namespace).

Have a look at the Application.Windows property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top