How can i intercept Escape button in dialog form?
-
Hi, I'm just moving from MFC to .NET. In MFC, I can easily intercept any message with PreTranslateMessage and do whatever i want with a particular msg. This is especially useful if a user presses the Escape button in a dialog. before all input is gone, i can ask the user if he/she really wants to proceed and discard all input. Should be no problem in .NET neither, i thought. But either it'S so obvious i just can't see it or it's not so easy after all(which would be a surprise since this is a frequently occurring scenario). What i do in respect to the Escape Button: it's assigned the DialogResult.Cancel value and the containing form sets this button's variable name as CancelButton. Any suggestions from someone? Thank's in advance! Wolfgang Puelm
-
Hi, I'm just moving from MFC to .NET. In MFC, I can easily intercept any message with PreTranslateMessage and do whatever i want with a particular msg. This is especially useful if a user presses the Escape button in a dialog. before all input is gone, i can ask the user if he/she really wants to proceed and discard all input. Should be no problem in .NET neither, i thought. But either it'S so obvious i just can't see it or it's not so easy after all(which would be a surprise since this is a frequently occurring scenario). What i do in respect to the Escape Button: it's assigned the DialogResult.Cancel value and the containing form sets this button's variable name as CancelButton. Any suggestions from someone? Thank's in advance! Wolfgang Puelm
When you drop a button on the form, you can then set the CancelButton property of your form to that button. It means that when escape is pressed the event handler of that button will be invoked. The dialogresult for that button will be set to Cancel.
#region signature my articles #endregion
-
When you drop a button on the form, you can then set the CancelButton property of your form to that button. It means that when escape is pressed the event handler of that button will be invoked. The dialogresult for that button will be set to Cancel.
#region signature my articles #endregion
Hi, thank you very much for your propmpt answer! But I'm afraid that's not my problem since the default event handler gives me no chance to remove this event (ESC pressed) from the form's message processing queue. That means the dialog will be inevitably be closed and that's what i'm trying to prevent if desired by user input. If this handler had a KeyEventArgs parameter instead of a EventArgs, the KeyEventArgs.Handled property could be set to true which might do the job (if it's passed by reference). Unfortunately, the event handler is implemented as it is. Once again, thank you for your effort! Regards Wolfgang Puelm
-
Hi, I'm just moving from MFC to .NET. In MFC, I can easily intercept any message with PreTranslateMessage and do whatever i want with a particular msg. This is especially useful if a user presses the Escape button in a dialog. before all input is gone, i can ask the user if he/she really wants to proceed and discard all input. Should be no problem in .NET neither, i thought. But either it'S so obvious i just can't see it or it's not so easy after all(which would be a surprise since this is a frequently occurring scenario). What i do in respect to the Escape Button: it's assigned the DialogResult.Cancel value and the containing form sets this button's variable name as CancelButton. Any suggestions from someone? Thank's in advance! Wolfgang Puelm
For this I would normally handle the
FormClosing
event. You'll then get the opportunity to find out why the form is closing by checking theDialogResult
property. You should normally make the Cancel button, the Escape key, and the X button in the top-right corner all do the same thing. If you really want to go down thePreTranslateMessage
route, you can implement theIMessageFilter
interface and useApplication.AddMessageFilter
to have the framework call your filter function for each message.
DoEvents
: Generating unexpected recursion since 1991 -
For this I would normally handle the
FormClosing
event. You'll then get the opportunity to find out why the form is closing by checking theDialogResult
property. You should normally make the Cancel button, the Escape key, and the X button in the top-right corner all do the same thing. If you really want to go down thePreTranslateMessage
route, you can implement theIMessageFilter
interface and useApplication.AddMessageFilter
to have the framework call your filter function for each message.
DoEvents
: Generating unexpected recursion since 1991Hi, Thanks a lot for your answer! It took me on the right track - at last: The xxx_FormClosing event has a FormClosingEventArgs parameter. From the MSDN i found out that there is a CancelEventArgs param also. Since it's possible to cast the FormClosingEventArgs into a CancelEventArgs, the latter's Cancel property can be used: if set true, the dialog stays alive, other it get's closed! :-D Thank's again! :rose: Regards. Wolfgang Puelm