How to unload the forms?

Hi,
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]
    Dim
    For I = 0 To Forms.Count - 1
        Unload Forms(I)
    Next

Thanks.
LVL 5
KarcOriginAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Timbo87Commented:
If you want to exit the application you can use Application.Exit().
0
arif_eqbalCommented:
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
            Frm.Close()
        Next

If the forms are not MDI children they'd probably be Modal, It depends you have both the options Application.Exit or Loop Through
0
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!
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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.

~IM
0
arif_eqbalCommented:
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
eg
Dim Frm as New form1
Frm.Owner=Me 'Or the Main Start up Form
Frm.Show

Now you can use

 Dim frm As Form
        For Each Frm In Me.OwnedForms
            frm.Close()
        Next
in the Start Up form to close all windows
0
Mike TomlinsonMiddle 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
        List.Add(frm)
    End Sub

    Public ReadOnly Property Item(ByVal index As Integer) As Form
        Get
            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)
        list.Remove(sender)
    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
        frms.Add(f2)
        f2.Show()
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        While frms.Count > 0
            frms.Item(0).Close()
        End While
    End Sub
End Class
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.