I am currently developing an application in c# using wpf. I would like to control what the red x (close window) does. At the moment, I press the red x and it closed the current window and continues running the rest of the program. I do not want this to h开发者_运维技巧appen. Instead when the x is pressed I want the whole program to finish.
Can this be done?
Thanks for your help
You want to do some event handling on the Window.Closed event, or override the Window.OnClosed method and put the code to shutdown the program in there.
http://msdn.microsoft.com/en-us/library/system.windows.window.onclosed.aspx
An alternative to DGH's answer can also be found on codeproject's website
You probably want to trap the closing of the form, and set a global such as "stopRequested", that other parts of the program should check while looping. See http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx
精彩评论