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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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?
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.


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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

632 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