Modaldialog doesn't react to enter/esc

不羁岁月 提交于 2019-12-10 03:17:14

问题


I have a modaldialog with an OK and a Cancel button. For the OK I set the Default property to True, and for the Cancel button the Cancel property. ModalResult is set to mrOK and mrCancel, resp.

However neither pressing the Enter nor the Esc key on my keyboard close the dialog. What did I miss here?

edit
I posted a small test application using the suspect dialog on my site. IDE is RAD Studio XE3.


回答1:


From your posted example you can see that the TSpinEdit control is focused and captures the keys.

To close the modal form in all cases, set form KeyPreview to true and insert this into the OnKeyPress event:

procedure TSelectDlg.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if (Key = Char(vk_escape)) then  // #27
    CancelBtn.Click
  else
  if (Key = Char(vk_return)) then  // #13
    OkBtn.Click;    
end;



回答2:


For the record, this should work. However, it seems that TSpinEdit has a bug. Since TSpinEdit is a sample (Vcl.Samples.Spin.pas, note the "Samples"), you can fix this yourself.

To TSpinEdit, add the following method declaration just following WMCut:

   procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;

Complete the class (Shift+Ctrl+C) and add the following code to WMGetDlgCode:

procedure TSpinEdit.WMGetDlgCode(var Message: TWMGetDlgCode);
begin
  inherited;
  Message.Result := Message.Result and not DLGC_WANTALLKEYS;
end;

That will tell VCL that the edit control doesn't want to process the Enter and Escape keys (VK_ENTER, VK_ESCAPE). Since it doesn't process the keys, they'll be forwarded to the buttons, which will then be invoked base on their settings (Default & Cancel).

Feel free to report this at Quality Central



来源:https://stackoverflow.com/questions/24584190/modaldialog-doesnt-react-to-enter-esc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!