Standard way to close all forms


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Standard way to close all forms

  1. #1
    Join Date
    Dec 2003
    Posts
    3

    Standard way to close all forms

    Hi all,
    I am using VS 2005 (.NET 2.0). However, I am fairly new to .NET so please bear with me. In VB6 it was pretty standard to have this in the cmdExit_Click event on frmMain:

    Private Sub cmdExit_Click()
    Dim frm As Form
    For Each frm In Forms
    Unload frm
    Next frm
    End Sub

    I've been searching high and low for a generic, standard way to close all open forms in a Windows Application, but every example I find is different. I finally came up with this and it seems to work but I just wanted to get your feedback. Do you see anything wrong with doing it this way? Is there a simpler way? I already tried looping over the OpenForms collection using a For-Each loop, but it throws an error when you close the first form.

    Thanks.

    ----------
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()
    End Sub

    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim f As Windows.Forms.Form
    Dim i As Integer = My.Application.OpenForms.Count - 1
    Try
    Do While i > 0
    'Close any other forms that may be open before frmMain closes...
    f = My.Application.OpenForms.Item(i)
    If f.Name <> Me.Name Then
    f.Close()
    i -= 1
    End If
    Loop
    Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.OkOnly)
    Finally
    f = Nothing
    End Try
    End Sub

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    If your app has a main form (that is, you specify a startup form in your project's Application settings), you no longer need to write code to close all the other forms in your app. When the main form closes, all other forms close automatically.

    Try this: create a new Windows Forms project; by default, Form1 becomes the startup form. Add the following code to the form:
    Code:
    Public Class Form1
        Private Sub Form1_Click(ByVal sender As Object, _ 
            ByVal e As System.EventArgs) Handles Me.Click
            Dim frm As New Form1
            frm.Show()
        End Sub
    End Class
    This will create and show a new instance of the form each time you click on it. Now run the project; click on the form a few times to open multiple windows. Now close the first form and watch what happens.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Dec 2003
    Posts
    3
    Very cool! So all I need is Me.Close in the btnExit_Click event.

    Thanks!

Similar Threads

  1. How do I close instance forms
    By Jacky in forum VB Classic
    Replies: 4
    Last Post: 08-13-2001, 07:54 AM
  2. How do I close instance forms
    By Jacky in forum VB Classic
    Replies: 0
    Last Post: 08-10-2001, 11:38 PM
  3. Problem in Access 97 Using Forms and Sub Forms
    By Barend Esterhuizen in forum VB Classic
    Replies: 1
    Last Post: 06-16-2000, 04:39 AM
  4. Replies: 2
    Last Post: 03-14-2000, 12:07 PM
  5. Replies: 0
    Last Post: 03-14-2000, 11:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center