How to unload the forms?

Posted on 2004-10-27
Last Modified: 2008-02-14
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)

Question by:KarcOrigin
    LVL 15

    Expert Comment

    If you want to exit the application you can use Application.Exit().
    LVL 19

    Expert Comment

    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
    LVL 5

    Author Comment

    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!
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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.

    LVL 19

    Expert Comment

    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
    LVL 85

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    The Complete Ruby on Rails Developer Course

    Ruby on Rails is one of the most popular web development frameworks, and a useful tool used by both startups and more established companies to build strong graphic user interfaces, and responsive websites and apps.

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now