Solved

usercontrol closing event

Posted on 2006-06-09
11
2,508 Views
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!

0
Comment
Question by:kevinmeredith
  • 4
  • 2
  • 2
  • +1
11 Comments
 
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."

0
 

Author Comment

by:kevinmeredith
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
            writeFileLog()
            userLog(Filename & ".mhl - " & "Stopped - Program Shut Down")
        End If
        MyBase.Finalize()
    End Sub

0
 
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?
0
 

Author Comment

by:kevinmeredith
ID: 16874053
yeah, the breakpoint is never hit.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 34

Expert Comment

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

http://msdn2.microsoft.com/en-us/library/hks5e2k6.aspx

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".

;-)

Roger
0
 
LVL 11

Expert Comment

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

0
 

Author Comment

by:kevinmeredith
ID: 16885770
i'll try that Roger.  thanks

how would you hook that anyoneis?
0
 

Author Comment

by:kevinmeredith
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.  


Form1:  
    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

UserControl:
    Private WithEvents form_Closing As Form1

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

    End Sub
0
 
LVL 11

Accepted Solution

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

[code]
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

[/code]
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now