How did the user close the form?
-
Hi, I'm doing a windows app and I need to know if the user pushed the red X button in the top right of my form. I know the Form_closing has an enum telling the reason of the form close, but it doesnt specify if it was my own cancel button or the X button, and I need to know it. Any ideas? Thanks in advance
-
Hi, I'm doing a windows app and I need to know if the user pushed the red X button in the top right of my form. I know the Form_closing has an enum telling the reason of the form close, but it doesnt specify if it was my own cancel button or the X button, and I need to know it. Any ideas? Thanks in advance
public class MyForm:Form { private bool _cancelButtonClicked = false; private void cancelbutton_clicked(..) { _cancelButtonClicked = true; } private void form_closing(..) { if (_cancelButtonClicked) { //User clicked the button with the dialog result == cancel } else { //User clicked X button to close the form } } }
- Malhar -
public class MyForm:Form { private bool _cancelButtonClicked = false; private void cancelbutton_clicked(..) { _cancelButtonClicked = true; } private void form_closing(..) { if (_cancelButtonClicked) { //User clicked the button with the dialog result == cancel } else { //User clicked X button to close the form } } }
- Malhar -
Yeah, well...thanks for the reply, but I was looking for a non-flag solution, thats the easy way :)
That's also the only way you can tell the difference. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
That's also the only way you can tell the difference. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
hi chals! :) try this:
private const int WM_SYSCOMMAND = 0x112;
private const int SC_CLOSE = 0xF060;protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE)
{
//do something here
}
}messages under WM_SYSCOMMAND includes: - Restore, Move, Size, Minimize, Maximize & Close (x button in the forms) or the commands in the context menu when you right click the title bar of a window. the SC_CLOSE WParam is for the close command. you can look for other commands in the WinUser.h file of Visual Studios. most of the time it is in "C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\" folder. It has the list of constants for API commands. hope that helps! :) microsoc :cool:
-
hi chals! :) try this:
private const int WM_SYSCOMMAND = 0x112;
private const int SC_CLOSE = 0xF060;protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE)
{
//do something here
}
}messages under WM_SYSCOMMAND includes: - Restore, Move, Size, Minimize, Maximize & Close (x button in the forms) or the commands in the context menu when you right click the title bar of a window. the SC_CLOSE WParam is for the close command. you can look for other commands in the WinUser.h file of Visual Studios. most of the time it is in "C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\" folder. It has the list of constants for API commands. hope that helps! :) microsoc :cool: