How do I unload all open forms in VB.NET?
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).
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, 2020Comments
-
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 withClose
(as indicated by TFM), but the compiler complains thatOpenForms
is not a member ofMy.Application
.Where can I access the open forms?
-
Franci Penov over 15 yearspulling out the power plug will achieve the desired result as well. nevermind the few side effects... :-)))
-
brasskazoo over 15 yearsIts 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 over 15 yearsThe 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 over 15 yearsIt's in the VS 2008 documentation. msdn.microsoft.com/en-us/library/eh13dca9.aspx
-
MarkJ over 15 yearsJust tried it in VB2008 EXpress Edition. It works for me - does exactly what it says on the tin
-
Cerebrus over 15 yearsThis is another way of applying for-each (with a slight performance difference for large number of iterations).
-
brasskazoo over 15 yearsThe error is: 'OpenForms' is not a member of 'i_hi002.My.MyApplication'.
-
Lucas Jones about 15 yearsYou can just use Application.OpenForms, as usually you have 'Imports System.Windows.Forms' anyway.
-
brasskazoo about 15 yearsAhh... Thats what I was missing, the import!
-
brasskazoo about 15 yearsSo All I was missing (thanks to person-b's comment) was the import - 'Imports System.Windows.Forms'!
-
Cody C over 12 yearsAlso, 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 over 12 yearsI get a collection modified exception sometimes when doing this.
-
Carl Onager almost 12 yearsThis worked for me in VS2010 when Imports System.Windows.Forms didn't
-
yu_ominae over 11 yearsI 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 over 9 yearsThanks, 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 about 9 yearsDitto the comment of @AjV Jsy for me. I am using Visual Studio 2013.