Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to unload the forms?

Posted on 2004-10-27
6
Medium Priority
?
6,193 Views
Last Modified: 2008-02-14
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.
0
Comment
Question by:KarcOrigin
6 Comments
 
LVL 15

Expert Comment

by:Timbo87
ID: 12430477
If you want to exit the application you can use Application.Exit().
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12430567
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
 
LVL 5

Author Comment

by:KarcOrigin
ID: 12430641
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12430686
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
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12430731
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 12430823
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

578 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