473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Closing an application properly

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
10 1806
* "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
"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
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
* "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
"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
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
"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
* "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
"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
* "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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Theo | last post by:
Question for the group The authentication system for the site Im working on seems to function properly and all is good. A session keeps track of everything and a cookie is used to accept or deny...
11
by: danny | last post by:
Is there a way I can prevent the browser window from being closed? I'd like to make sure the browser window only closes programatically (I want to make sure the user enters data before moving on)....
16
by: ThunderMusic | last post by:
Hi, My app does not stop whan I click on the form. I mean, the form is closing, but the process keeps running in the task manager. So I figured there are memory leaks or some object's process...
5
by: Ron L | last post by:
I have an MDI application with a number of child windows. In each child window I am catching the Closing event and having the child window decide if it should set cancel to true. The intent here...
1
by: Chris Bruce | last post by:
In my application I need a way to distiguish between the following events: 1. When a user closes an MDI child window. 2. When the user closes the MDI parent window which subsequently closes the...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
3
by: Charles Law | last post by:
Under what circumstances would e.Cancel be set to True on entry to the Closing event of an MDI child form? I have found that this is why my application won't close properly. I can explicitly set...
2
by: Atley | last post by:
I have written an application that exports data from SQL to Excel. It all works perfectly except that if you open the Task Manager after running my application, there is an instance of Excel in...
6
by: Giojo | last post by:
Hello guys! I can't resolve this problem.. I want my programm in c# working with only console if there are some parameters, but if someone make double-click on the exe I want to start the graphic...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.