473,761 Members | 9,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1831
* "Ricky W. Hunt" <rh*****@hotmai l.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******** ******@TK2MSFTN GP11.phx.gbl...
* "Ricky W. Hunt" <rh*****@hotmai l.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******** ******@TK2MSFTN GP11.phx.gbl...
* "Ricky W. Hunt" <rh*****@hotmai l.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*****@hotmai l.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******** *******@tk2msft ngp13.phx.gbl.. .
* "Ricky W. Hunt" <rh*****@hotmai l.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(B yVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing,
btnExit.Click, MenuItem3.Click

If Not Nothing Is applicationBuff er Then
If applicationBuff er.Status.Playi ng = True Then
applicationBuff er.Stop()
applicationBuff er.SetCurrentPo sition(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*****@hotmai l.com> wrote in message
news:3jCLc.1474 $eM2.6@attbi_s5 1...
Oops. Sorry, one more question.
OK. Here's the code I ended up with:

Private Sub Form1_Closing(B yVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) 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*****@hotmai l.com> scripsit:
OK. Here's the code I ended up with:

Private Sub Form1_Closing(B yVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing,
btnExit.Click, MenuItem3.Click

If Not Nothing Is applicationBuff er Then
If applicationBuff er.Status.Playi ng = True Then
applicationBuff er.Stop()
applicationBuff er.SetCurrentPo sition(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******** *****@TK2MSFTNG P11.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
2884
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 access... sounds as it should be I hope. Then when all is done logging out destroys everything and the pages are no longer accessable. Good so far. But two things come to mind: one is preventing multiple logins of the user/password. The...
11
2552
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). Another possible solution would be to re-open the window when it's closed. Any help is appreciated, thanks.
16
1932
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 stuck at some point. Is there an easy way to know where it comes from other than putting breakpoints all over the code? 'cause it's huge and I would have to set breakpoints for at least a day or two thanks
5
2260
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 is to ensure that no child window can close while it is in a state where user entered information can be lost. I have just noticed that while the Closing event is caught if I click the X on the child window, it is not caught if I click the X on...
1
5821
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 MDI child window. My application does certain logic when the user actually closes the MDI child form by clicking the "X" in the upper right hand. My application, however, should not execute this logic if the user closes the MDI parent. I...
10
4029
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 application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
3
2776
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 the value to False, but I would have expected it to be False on entry. TIA Charles
2
2270
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 the list for each time the extract has been run. I cannot seem to find anything on this, I have the application closing properly as far as I can see, but it is staying in the process table and the only way to get rid of it is to either reboot...
6
10433
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 GUI without the black console on background.. Now I check if there are some parameters and if there aren't parameters I run the GUI (with the black console that remain..). If I choose Windows application on Visual Studio, when I run the...
0
9531
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.