简体   繁体   English

如果表单中的文本框具有自动完成功能,onKeyPress事件不起作用吗?

[英]onKeyPress event do not work if a textbox in form has autocomplete?

I have a form with keypreview true . 我有一个带有keypreview true的表单。 OnKeyPress (if key is escape) I was closing the form. OnKeyPress (如果键已转义)我正在关闭表单。

Later I set autocomplete with the first text box in form. 稍后,我使用表单中的第一个文本框设置autocomplete Autocomplete is working but OnKeyPress event is not working now. 自动完成功能正在工作,但OnKeyPress事件现在无法工作。 If cursor is not in text box having auto-complete OnKeyPress works and still close the form on escape key. 如果光标不在具有自动完成功能的文本框中,则OnKeyPress仍然有效,并且仍按OnKeyPress键关闭表单。

Please guide how I can have both ? 请指导我如何同时拥有两者? Autocomplete with closing form on escape. Autocomplete ,转义时关闭表格。

In your application you should not use form events for closing (because any control on your form can handle OnKeyPress by it's own and swallow it), you should register the hotkey. 在您的应用程序中,您不应该使用窗体事件来关闭(因为窗体上的任何控件都可以自己处理并吞下OnKeyPress ),您应该注册热键。

[DllImport("user32.dll")]
private static extern bool RegisterHotKey (int hwnd, int id, int fsModifiers, int vk);

[DllImport("user32.dll")]
private static extern bool UnregisterHotKey (int hwnd, int id);

Refer to here or to here 这里这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM