Shandy's Blog

Where Andrew Sutton, aka Shandy rants and rambles on as the fancy takes him

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

My name is Andrew Sutton, aka Shandy.

I am currently living and working in the UK as a software developer. This blog contains mainly IT related issues.

I was a Microsoft VB MVP for a couple of years (Apr 2004-Mar 2006) and was a vbCityLeader between April 2003 and June 2007.

If you are looking for my Sri Lanka or Morocco experiences check out Shandy's Sri Lanka Blog or Shandy's Morocco Blog. My personal (Non IT) blog is now at Shandy's Place

OctNovember 2009Dec
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Articles

Archives

Topics

Image Galleries

.NET

Blogs I Read

Others

VB.Classic

How To Get The Forms Closing And Closed Events To Execute When Exiting An Application

By Andrew Sutton, Microsoft VB MVP & vbCity Leader
Article Information

Language:

IDE:

Framework:

Area:

Audience:

VB.NET

Any

1.x

Windows.Forms (Controls)

Beginner, Intermediate, Expert

The Problem

For some unknown reason (to me at least) when you execute an Application.Exit call in VB.NET 2003 the closing and closed events for any forms in an application are not executed. This article gives one possible way of working around this issue so that on exiting an application all the form closing and closed events are executed.

The Solution

You will need to ensure that your project starts with a Sub Main and not a form. The following code could become your startup module.

Module & Class Code

Module Startup
  Private oApplicationFormsCollection As New FormsCollection
  Public Sub Main()
    Dim oMain As New frmEntry(True) ' Where frmEntry is the name of your initial form
    oMain.Show()
    Application.Run()
  End Sub
  Public Sub ExitApplication()
    Dim oForm As Form
    Do While oApplicationFormsCollection.Count > 0
      oForm = oApplicationFormsCollection.Item(oApplicationFormsCollection.Count - 1)
      oApplicationFormsCollection.Remove(oApplicationFormsCollection.Count - 1)
      oForm.Close()
    Loop
    Application.Exit()
  End Sub
  Public Sub RegisterLoadedForm(ByVal oForm As Form)
    oApplicationFormsCollection.Add(oForm)
  End Sub
  Private Class FormsCollection
    Inherits System.Collections.CollectionBase
    Public ReadOnly Property Item(ByVal iIndex As Integer) As Form
      Get
        Return CType(List.Item(iIndex), Form)
      End Get
    End Property
    Public Sub Add(ByVal oForm As Form)
      List.Add(oForm)
    End Sub
    Public Sub Remove(ByVal iIndex As Integer)
      If iIndex < 0 Then
        Throw New ArgumentException("iIndex " & CStr(iIndex) & "Must Be a Non-Negative Value")
      ElseIf iIndex > Count - 1 Then
        Throw New ArgumentException("iIndex (" & CStr(iIndex) & ") Must Be <= The Form Count (" & CStr(Count) & ")")
      Else
        List.RemoveAt(iIndex)
      End If
    End Sub
  End Class
End Module

The following code will need to be added to every form that you want to execute the form closing and closed events on when exiting an application.

Form Code

Public Sub New(ByVal bRegisterForm As Boolean)
  Call Me.New()
  If bRegisterForm Then
    Startup.RegisterLoadedForm(Me)
  End If
End Sub

Note that every form that you want to execute the closing and closed events on should included the above code and be instantiated using Dim oForm as New FormName(True), where FormName is the name of your form.


posted on Saturday, July 09, 2005 4:23 PM