DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2003

    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.


    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    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
    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
    i -= 1
    End If
    Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.OkOnly)
    f = Nothing
    End Try
    End Sub

  2. #2
    Join Date
    Nov 2003
    Portland, OR
    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:
    Public Class Form1
        Private Sub Form1_Click(ByVal sender As Object, _ 
            ByVal e As System.EventArgs) Handles Me.Click
            Dim frm As New Form1
        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

    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
    Very cool! So all I need is Me.Close in the btnExit_Click event.


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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.