usercontrol closing event

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!

Who is Participating?
anyoneisConnect With a Mentor Commented:
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

Brian CroweDatabase AdministratorCommented:
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."

kevinmeredithAuthor Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Brian CroweDatabase AdministratorCommented:
by "didn't work" do you mean it never executed?  If you put a breakpoint in the procedure does it ever hit it?
kevinmeredithAuthor Commented:
yeah, the breakpoint is never hit.
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".


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

kevinmeredithAuthor Commented:
i'll try that Roger.  thanks

how would you hook that anyoneis?
kevinmeredithAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.