usercontrol closing event

Posted on 2006-06-09
Last Modified: 2010-08-05
I'm used to using the Form.Closing event when working with forms.  I was wondering if there was anything similar to usercontrols.  I have a winApp that uses 8 different usercontrols that have timers.  if the timers are on, and the program gets shut down, i want to have it so it will write to a premade log that the program shutdown without turning off the timers.  forms had this event but usercontrols don't.  is there an easy way to do this?  thanks!

Question by:kevinmeredith
  • 4
  • 2
  • 2
  • +1
LVL 34

Expert Comment

by:Brian Crowe
ID: 16873899
Try adding a finalize sub to your usercontrol.

"When an object is no longer needed, the CLR calls the Finalize method for that object before freeing its memory. The Finalize method is called a destructor because it performs cleanup tasks, such as saving state information, closing files and connections to databases, and other tasks that must be done before releasing the object.

When the Sub Finalize method of a derived class is called, it first performs any cleanup tasks that are needed and then uses the syntax MyBase.Finalize() to explicitly call the Sub Finalize method of its base class. Therefore, Sub Finalize methods run from the most-derived classes first, and execute code in the base class last."


Author Comment

ID: 16873961
here is what i put in and it didn't work.  any idea why?  writeFileLog() and UserLog() are subs that i write files too. thanks

 Protected Overrides Sub Finalize()
        If Timer1.Enabled Then
            userLog(Filename & ".mhl - " & "Stopped - Program Shut Down")
        End If
    End Sub

LVL 34

Expert Comment

by:Brian Crowe
ID: 16874046
by "didn't work" do you mean it never executed?  If you put a breakpoint in the procedure does it ever hit it?
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 16874053
yeah, the breakpoint is never hit.
LVL 34

Expert Comment

ID: 16874503
Here's a description of the problem you're facing.  It's from

towards the end of the page.

The CLR periodically destroys objects when the system determines that such objects are no longer needed. Objects are released more quickly when system resources are in short supply, and less frequently otherwise. The delay between when an object loses scope and when the CLR releases it means that, unlike with objects in Visual Basic 6.0 and earlier versions, you cannot determine exactly when the object will be destroyed. In such a situation, objects are said to have non-deterministic lifetime. In most cases, non-deterministic lifetime does not change how you write applications, as long as you remember that the Finalize destructor may not immediately execute when an object loses scope.

If you follow the How To: link at the bottom of the page, it looks to me (although I haven't tried it) as though it may provide what you want.  You don't say which version / Net framework you are using.  But although the above link and quote are for VB.NET 2005, the approach in VB.NET 2003 looks similar.

The above, by the way, is not claimed to be any sort of "expert" knowledge on my part.  It's just that it looked interesting, so I did a test, and found Finalize in my user control didn't get hit, so I started digging in the help files.  And then it started to look a bit too complicated for me to take my tests any further "just for interest".


LVL 11

Expert Comment

ID: 16875557
What if you have your user controls hook this.Parent.Closing when they are loaded?


Author Comment

ID: 16885770
i'll try that Roger.  thanks

how would you hook that anyoneis?

Author Comment

ID: 16886296
Here is what i'm using to try to hook the usercontrol from the main form's closing event.  does this look right?  i can't get it to work this way.  anyway help is greatly appreciated.  

    Public Event window_closing(ByVal st As Integer)

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        RaiseEvent window_closing(4)

    End Sub

    Private WithEvents form_Closing As Form1

    Public Sub win_closing(ByVal st As Integer) Handles form_Closing.window_closing
        MsgBox("closing test")

    End Sub
LVL 11

Accepted Solution

anyoneis earned 500 total points
ID: 16897436
How about something like this:

Imports System.ComponentModel
Public Class UserControl1

    Private m_MyForm As Form = Nothing

    Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ctl As Control = Me.Parent
        While Not TypeOf ctl Is Form AndAlso ctl.Parent IsNot Nothing
            ctl = ctl.Parent
        End While
        If TypeOf ctl Is Form Then
            m_MyForm = DirectCast(ctl, System.Windows.Forms.Form)
            AddHandler m_MyForm.Closing, AddressOf MyFormIsClosing
        End If
    End Sub

    Private Sub MyFormIsClosing(ByVal sender As System.Object, ByVal e As CancelEventArgs)
        If m_Myform IsNot Nothing Then
            RemoveHandler m_MyForm.Closing, AddressOf MyFormIsClosing
        End If
    End Sub

End Class


Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…

821 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