By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,815 Members | 1,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,815 IT Pros & Developers. It's quick & easy.

Closing an application properly

P: n/a
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?

--
Thanks,
Ricky W. Hunt
freendeed
Nov 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
* "Ricky W. Hunt" <rh*****@hotmail.com> scripsit:
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 [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #2

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ue**************@TK2MSFTNGP11.phx.gbl...
* "Ricky W. Hunt" <rh*****@hotmail.com> scripsit:
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.


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.
Nov 20 '05 #3

P: n/a
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
--
David Williams, VB.NET MVP
"Ricky W. Hunt" wrote:
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ue**************@TK2MSFTNGP11.phx.gbl...
* "Ricky W. Hunt" <rh*****@hotmail.com> scripsit:
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.


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.

Nov 20 '05 #4

P: n/a
* "Ricky W. Hunt" <rh*****@hotmail.com> scripsit:
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.


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 ;-).
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.


OK, then forget this last sentence.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #5

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
* "Ricky W. Hunt" <rh*****@hotmail.com> scripsit:
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.


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 ;-).


OK. Thanks.
Nov 20 '05 #6

P: n/a
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?
Nov 20 '05 #7

P: n/a
"Ricky W. Hunt" <rh*****@hotmail.com> wrote in message
news:3jCLc.1474$eM2.6@attbi_s51...
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.
Nov 20 '05 #8

P: n/a
* "Ricky W. Hunt" <rh*****@hotmail.com> scripsit:
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 [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #9

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ec*************@TK2MSFTNGP11.phx.gbl...

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?
Nov 20 '05 #10

P: n/a
* "Ricky W. Hunt" <rh*****@hotmail.com> scripsit:
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?


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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.