views:

1252

answers:

6

I'm having a rather perplexing problem with the Escape key handler on a dialog box in Borland C++ Builder 5. Are there any other requirements for the Escape key to fire a cancel event (other than those I've listed below)?

  1. The "Cancel" button (a TBitBtn) has its Cancel property set to true.
  2. The "Cancel" button has its Default property set to false.
  3. The "Cancel" button has its modalResult set to mrCancel.

Note: I'm working with an old legacy app that is still being compiled in Borland C++ Builder 5. We have a separate project to replace it - I'm just doing end of life maintenance.

Update

Four months later I've finally stopped scratching my head...it turns out that the parent form for the application had a run-time OnShortCut handler defined. I just needed to disable that for the Esc handler to work on the child dialog.

+1  A: 

You should check all possible ways the cancel event could be blocked:

  1. First of all, check if clicking the cancel button actually closes the form.

  2. Then check if any other button has its Cancel property set to true.

  3. After that check all key event handlers, don't forget the event handlers of the form, especially if you have KeyPreview enabled.

  4. If you still don't find the problem, check if another component has its ShortCut property set to handle the escape key.

  5. Also check if there are any keyboard hooks active.

DR
1. yep, works.2. nope3. this particular form has no key event handlers definedthanks for the pointers
Will Bickford
+1  A: 

It may be that the Form's KeyPreview property has been set to true. This is where the Escape key is often/likely to have been disabled. The KeyPreview property is is also often enabled to capture [Return] key press (I.e. OnKeyPress) to advance to the next field rather than closing the form.

Roger Nelson
I'll check this out tomorrow when I'm back @ work. Thanks.
Will Bickford
+1  A: 

You can also create another dialog, add Cancel button into it and see if the Escape key works. Then compare the DFM source of both forms and check the differences in settings.

Riho
A: 

Is there a "CanClose" type event with logic preventing it from closing?

Ray Hayes
+1  A: 

Also keep in mind that the dialog needs to be invoked via ShowModal() rather than just Show(). For example, if your form is named "FAskDialog" then the code that displays it should be like

FAskDialog->ShowModal();

rather than

FAskDialog->Show();

If the dialog is invoked via Show(), then hitting a cancel key or setting ModalResult = mrCancel will NOT cause the dialog to close.

Kluge
Checked this too - ShowModal is being used unfortunately. Thanks though.
Will Bickford
A: 

hi this is an old question, I can give answer:

You need to set KeyPreview = True and then write Code to trap the ESC key:

Form1::OnKeyUp(...)
{
if (Key == 27) {
Close();
}
}

buttercup