Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

usercontrol closing event

Posted on 2006-06-09
11
Medium Priority
?
2,924 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
9 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:kevinmeredith
ID: 16874053
yeah, the breakpoint is never hit.
0
 
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 1500 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

773 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