Close event in winform

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

I have a vb.net 2.0 winforms app. In the old days there was a close event
that would fire when the form closes but I don't see any close or unload
events. Can you please tell me what event fires when the form closes so I
can run some cleanup code?

Thanks.
 
Hi Moondaddy,

The Disposed event of the Form occurs only when the Dispose method is
called on this form.

The Dispose method will be called automatically if the form is shown using
the Show method. If another method such as ShowDialog is used, or the form
is never shown at all, you must call Dispose yourself within your
application.

If you have some cleanup code to run when a Form is closed, the recommended
way is to place these cleanup code in the Form's protected Dispose(Boolean)
method within the Form.Designer.vb file. And then call the Dispose method
in the Form's FormClosed event handler if the Form is shown modally.

Note that the protected Dispose(Boolean) method is called by the public
Dispose method and the Finalize method. Dispose invokes the protected
Dispose(Boolean) method with the disposing parameter set to true. Finalize
invokes Dispose with disposing set to false. When the disposing parameter
is true, this method releases all resources held by any managed objects
that this Form references. This method invokes the Dispose method of each
referenced object.

So the complete solution is like following:

' Form1.Designer.vb
Partial Class Form1
Inherits System.Windows.Forms.Form

<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If

If disposing Then
' place the cleanup code here
...
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
...
End Class

' Form1.vb
Public Class Form1

Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
If (Me.Modal) Then
Me.Dispose()
End If
End Sub
...
End Class

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Private Sub MyForm_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
' Do your thing...
End Sub
 
Thanks this is all very helpful. First of all I couldn't fine the
formclosing event because I was looking for an event called Closed or
Closing. Also, thanks for setting me straight on the Dispose method.
 
Back
Top