[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to unload the forms?

Posted on 2004-10-27
6
Medium Priority
?
6,185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

649 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