【问题标题】:onKeyPress event do not work if a textbox in form has autocomplete?如果表单中的文本框具有自动完成功能,onKeyPress 事件不起作用?
【发布时间】:2011-01-20 09:20:10
【问题描述】:
我有一个带有keypreview true 的表格。 OnKeyPress(如果键是转义)我正在关闭表单。
后来我用表单中的第一个文本框设置autocomplete。自动完成功能正在运行,但 OnKeyPress 事件现在无法运行。如果光标不在具有自动完成OnKeyPress 的文本框中,并且仍然在转义键上关闭表单。
请指导我如何同时拥有两者? Autocomplete 在转义时关闭表格。
【问题讨论】:
标签:
c#
.net
winforms
.net-3.5
【解决方案1】:
在您的应用程序中,您不应该使用表单事件来关闭(因为您的表单上的任何控件都可以自己处理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);
参考here或here