How to unload the forms?

How can I unload the windows application gracefully? I mean unloading and closing all the opened forms in the application.

Consider the following example of classic VB 6.0 for a better idea and I need the equivalent code in VB.NET

[VB 6.0 code]
    For I = 0 To Forms.Count - 1
        Unload Forms(I)

Who is Participating?

Improve company productivity with a Business Account.Sign Up

Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Here is a very simple example of what I described:

Public Class Forms
    Inherits System.Collections.CollectionBase

    Public Sub Add(ByVal frm As Form)
        AddHandler frm.Closed, AddressOf Me.f_Closed
    End Sub

    Public ReadOnly Property Item(ByVal index As Integer) As Form
            If index >= 0 And index < list.Count Then
                Return CType(List.Item(index), Form)
            End If
        End Get
    End Property

    Private Sub f_Closed(ByVal sender As Object, ByVal e As System.EventArgs)
    End Sub
End Class

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private frms As New Forms

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f2 As New Form2
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        While frms.Count > 0
        End While
    End Sub
End Class
If you want to exit the application you can use Application.Exit().
Application.Exit is perhaps the easiest way out but it has one serious flaw

It does not fire the Form_Closing or From_Closed event of the Forms being Closed

So, say if you have clean up code in one of these events it won't get executed.

A good design would be to have all these forms as Child to an MDI form and then use

       Dim Frm As Form
        For Each Frm In Me.MdiChildren

If the forms are not MDI children they'd probably be Modal, It depends you have both the options Application.Exit or Loop Through
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

KarcOriginAuthor Commented:
Timbo87, I know that Application.Exit is not the gracefull way to unload the application. Nether it was a good design to call Exit in VB 6 nor I think it is good in VB.NET.

arif_eqbal, my application does not have the Parent-Child model (MDI) due to this I want some other way.

Cheer up!
Mike TomlinsonMiddle School Assistant TeacherCommented:
The proper way to do it would be to create your own class that gives you the same functionality as the Forms collection from VB6.  This means you would have to manually add each form as you instantiate it to your class so you can add it to your internal collection.  As each form is added, you can use AddHandler() to trap the Closed() event so it can be removed from the class collection (ArrayList or whatever you want to use) automatically.  When you want to end the app with a clean closing of all forms, just iterate the collection and call the Close() event for each form that is remaining.

One way as Idle Mind says...

You can even simplify it a bit by tracking all intances of forms in a collection in a module though

Another way can be to use Owned Forms
You have one start up form now anyway if this startup form closes the whole application closes. So probably you would like to place a code on the on this form to close all other open forms.

In this case what you can do is make all forms Owned by the strtup form
Dim Frm as New form1
Frm.Owner=Me 'Or the Main Start up Form

Now you can use

 Dim frm As Form
        For Each Frm In Me.OwnedForms
in the Start Up form to close all windows
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.