Overriding the Closing event
-
I have written a C# windows application. However, when the program is running, and I want to shut down windows, every program closes except my C# program; and windows does not shut down. It just waits. When I close my program manually, windows shuts down without a problem. The cause is that I have overriden the Closing event of the main form like this: private void WindowsCloserForm_Closing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; OpenDialog(false); } I wanted this behaviour however: closing the form should not end the program, but just hide it. (Via an icon in the system tray you can close the program) If I do not override the Closing event, this problem does not occur. Can this be a bug in the framework, or am I forgetting something? :rolleyes: Thanks!
-
I have written a C# windows application. However, when the program is running, and I want to shut down windows, every program closes except my C# program; and windows does not shut down. It just waits. When I close my program manually, windows shuts down without a problem. The cause is that I have overriden the Closing event of the main form like this: private void WindowsCloserForm_Closing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; OpenDialog(false); } I wanted this behaviour however: closing the form should not end the program, but just hide it. (Via an icon in the system tray you can close the program) If I do not override the Closing event, this problem does not occur. Can this be a bug in the framework, or am I forgetting something? :rolleyes: Thanks!
Windows is just doing what you asked it to :) You need to override the WndProc of the form and catch the WM_QUIT message, one of the parameters tells you what is telling the form to close; if its the user then you should continue as you do; otherwise let the program shutdown. Look up in MSDN what the values you need for WM_QUIT and what lParam and wParam are. Almost had me stumped on this one, then I remembered I tried doing the same in an MFC program and that was what I had to do :) James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
-
Windows is just doing what you asked it to :) You need to override the WndProc of the form and catch the WM_QUIT message, one of the parameters tells you what is telling the form to close; if its the user then you should continue as you do; otherwise let the program shutdown. Look up in MSDN what the values you need for WM_QUIT and what lParam and wParam are. Almost had me stumped on this one, then I remembered I tried doing the same in an MFC program and that was what I had to do :) James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
I hate it when I find things they have changed. VB used to have a nice event called QueryUnload, that told you why the form was closing (your code, user pressed X, windows shutting down etc.) and allowed to decide what to do. Why does Microsoft insist on doing things? Changing the way it works I dont mind, but removing functionality altogether? Makes me angry sometimes. </rant> -- David Wengier Sonork ID: 100.14177 - Ch00k