Closing an application properly

  • Thread starter Thread starter Ricky W. Hunt
  • Start date Start date
R

Ricky W. Hunt

I have written a close routine to handle an "Exit" button to close the
application properly. How do I make sure this gets executed if the user
closes it another way (by pressing the "X" in the upper right hand for
instance, or Alt+4, etc.) Or is this even necessary?
 
* "Ricky W. Hunt said:
I have written a close routine to handle an "Exit" button to close the
application properly. How do I make sure this gets executed if the user
closes it another way (by pressing the "X" in the upper right hand for
instance, or Alt+4, etc.) Or is this even necessary?

Add a handler to your form's 'Closing' event to handle the closing
process of a form. You can set 'e.Cancel' to 'False' there to cancel
closing the form.
 
Herfried K. Wagner said:
Add a handler to your form's 'Closing' event to handle the closing
process of a form.

So will the program always run this event regardless of how a program is
closed? Meaning, could I just put all of my wrapup code in the Closing event
and even let it handle someone pressing the Exit button?
You can set 'e.Cancel' to 'False' there to cancel
closing the form.

I'm not sure what that means. I know how to disable the "X" on the form
using the properties in design view. I want them to be able to close the
program with the X, I just want to make sure it runs my wrapup code.
 
What Herfried is saying is that if for some reason you decide in your code that the form should not be close, even though they pressed the "X" (for instance, you need them to save their data), then you can set the e.Cancel property to False and the form will not close.

HTH
 
* "Ricky W. Hunt said:
So will the program always run this event regardless of how a program is
closed? Meaning, could I just put all of my wrapup code in the Closing event
and even let it handle someone pressing the Exit button?

Yes. Notice that this sub is not executed if you /kill/ the process,
but there is AFAIK no way to handle that ;-).
I'm not sure what that means. I know how to disable the "X" on the form
using the properties in design view. I want them to be able to close the
program with the X, I just want to make sure it runs my wrapup code.

OK, then forget this last sentence.
 
Oops. Sorry, one more question.


OK. Here's the code I ended up with:

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing,
btnExit.Click, MenuItem3.Click

If Not Nothing Is applicationBuffer Then
If applicationBuffer.Status.Playing = True Then
applicationBuffer.Stop()
applicationBuffer.SetCurrentPosition(0)
Timer1.Enabled = False
End If
End If
Me.Close()
End Sub


Is this OK to handle all three events? (btnExit is just an exit button and
MenuItem3 is the way to exit from the menu).

It would seem to me if there's multiple ways to end the program (and Exit
button, the X button on the form, etc.), all the code should be in one place
like this. Is that correct or is there a better way?
 
Ricky W. Hunt said:
Oops. Sorry, one more question.


OK. Here's the code I ended up with:

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing,
btnExit.Click, MenuItem3.Click

It wouldn't let me do that (use btnExit.Click in the "Handles") I guess
they're not compatible.
 
* "Ricky W. Hunt said:
OK. Here's the code I ended up with:

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing,
btnExit.Click, MenuItem3.Click

If Not Nothing Is applicationBuffer Then
If applicationBuffer.Status.Playing = True Then
applicationBuffer.Stop()
applicationBuffer.SetCurrentPosition(0)
Timer1.Enabled = False
End If
End If
Me.Close()
End Sub


Is this OK to handle all three events? (btnExit is just an exit button and
MenuItem3 is the way to exit from the menu).

No! Handle 'MyBase.Closing' only (or override the base class'
'OnClosing' method). Inside the handlers of the menu item's 'Click'
event and the button's 'Click' event, simply call 'Me.Close()'. This
will cause the 'Closing' event to be raised.
 
Herfried K. Wagner said:
No! Handle 'MyBase.Closing' only (or override the base class'
'OnClosing' method). Inside the handlers of the menu item's 'Click'
event and the button's 'Click' event, simply call 'Me.Close()'. This
will cause the 'Closing' event to be raised.

Ah. I think I understand. I didn't realize Me.Close() was/triggered an
event. I thought that was the exit point of the program. So is "Closing" the
very last function that's executed before the program terminates?
 
* "Ricky W. Hunt said:
Ah. I think I understand. I didn't realize Me.Close() was/triggered an
event. I thought that was the exit point of the program. So is "Closing" the
very last function that's executed before the program terminates?

In most cases, yes. The 'Closed' event will be fired after the 'Closing' event.
 
Back
Top