How Do You Close All Forms That Are Opened in VB .NET

Posted on Posted in .NET

There is no direct method to close all forms in VB.NET.. (we had
the forms collection in VB6). Forms Collection is not supported in VB.NET which leaves the task of counting the forms in our hands..
(MICROSOFT’S way of upgrading things..).

The following code can solve ur problem.

*******PASTE THE FOLLOWING CODE INTO A MODULE.*****

Dim colFormCount As New Collection()

”To count the forms as and when they are loaded.
Sub countForm(ByVal frm As Form)
colFormCount.Add(frm)
End Sub

”To unload all the forms from the memory.
Sub UnloadAllForms()
Dim frmLoaded As Object

For Each frmLoaded In colFormCount
frmLoaded.close()
Next
End Sub
*************************************************

IN ORDER TO MAKE THE ABOVE CODE WORK, u have to call the COUNTFORM procedure each and every time u load a form.

for eg., when u load the form, in the FORM_LOAD event.. paste the following code.

COUNTFORM(ME) ”To increment the count.

Paste the code in all the form load events.

whenever u want to unload all the forms.. 
for eg., if u want to unload the forms in the click event of a button then, paste the following code in the button1_click event,

UNLOADALLFORMS()

Source:

Leave a Reply

Your email address will not be published. Required fields are marked *