How do I unload all open forms in VB.NET?

31,067

Solution 1

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

Solution 2

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

Solution 3

Application.Exit will pretty much do the same.

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

Solution 4

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

Share:
31,067
brasskazoo
Author by

brasskazoo

I am a Software and Cloud Solutions Engineer with a passion for building quality and automation into the development, testing and deployment lifecycles. Currently working with a large AWS hybrid-cloud integration project, including VPC architecting and solution design, serverless applications and shifting applications to EC2. Primarily I work with terraform, node.js, react and java, with side projects currently in react-native and GraphQL for cross-platform mobile applications. Agile, DevOps culture, Code Quality and Continuous Integration are cornerstones of my development efforts.

Updated on November 26, 2020

Comments

  • brasskazoo
    brasskazoo over 3 years

    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?

  • Franci Penov
    Franci Penov over 15 years
    pulling out the power plug will achieve the desired result as well. nevermind the few side effects... :-)))
  • brasskazoo
    brasskazoo over 15 years
    Its strange, I saw many people saying that this worked for them in VS 2005. My compiler refuses to acknowledge OpenForms (VS 2008)... Could it have been changed between the two versions??
  • MarkJ
    MarkJ over 15 years
    The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling Exit method.
  • MarkJ
    MarkJ over 15 years
    It's in the VS 2008 documentation. msdn.microsoft.com/en-us/library/eh13dca9.aspx
  • MarkJ
    MarkJ over 15 years
    Just tried it in VB2008 EXpress Edition. It works for me - does exactly what it says on the tin
  • Cerebrus
    Cerebrus over 15 years
    This is another way of applying for-each (with a slight performance difference for large number of iterations).
  • brasskazoo
    brasskazoo over 15 years
    The error is: 'OpenForms' is not a member of 'i_hi002.My.MyApplication'.
  • Lucas Jones
    Lucas Jones about 15 years
    You can just use Application.OpenForms, as usually you have 'Imports System.Windows.Forms' anyway.
  • brasskazoo
    brasskazoo about 15 years
    Ahh... Thats what I was missing, the import!
  • brasskazoo
    brasskazoo about 15 years
    So All I was missing (thanks to person-b's comment) was the import - 'Imports System.Windows.Forms'!
  • Cody C
    Cody C over 12 years
    Also, if you want to throw the user back to a login screen if they might do something like hit logout you might want to unload all of the forms but not exit the application.
  • Cody C
    Cody C over 12 years
    I get a collection modified exception sometimes when doing this.
  • Carl Onager
    Carl Onager almost 12 years
    This worked for me in VS2010 when Imports System.Windows.Forms didn't
  • yu_ominae
    yu_ominae over 11 years
    I converted the same code as the OP today and got a collection modified exception. In that case the old iterating through the collection backwards method works fine.
  • AjV Jsy
    AjV Jsy over 9 years
    Thanks, that works better for me than the For each approach which gave me an error as the .close modified the collection it was iterating over.
  • Guru Josh
    Guru Josh about 9 years
    Ditto the comment of @AjV Jsy for me. I am using Visual Studio 2013.