组合框 onkeypress 事件上的自动完成会占用 Enter 键
我有一个带有 AutoCompleteMode = suggest
的 ComboBox 并像这样处理 KeyPress 事件:
private void searchBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
// do stuff
}
}
但是,它没有捕获 Enter
键。它捕获了其他所有内容,因为自动完成下拉菜单工作得很好。
我还尝试了此处提供的建议: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/2db0b540-756a-4a4f-9371-adbb92409806,将表单的 KeyPreview
属性设置为true 并在表单的 KeyPress 事件处理程序中放置一个断点:
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = false;
}
但是,即使表单的处理程序也没有捕获回车键!
有什么建议吗?
(如果我禁用自动完成功能,它会捕获 Enter 键)
I have a ComboBox with AutoCompleteMode = suggest
and handle the KeyPress event like so:
private void searchBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
// do stuff
}
}
However, it does not catch the Enter
key. It catches everything else since the autocomplete dropdown works perfectly.
I also tried the suggestion offered here : http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/2db0b540-756a-4a4f-9371-adbb92409806, set the form's KeyPreview
property to true and put a breakpoint in the form's KeyPress event handler:
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = false;
}
However, even the form's handler was not catching the enter key!
Any suggestions?
(If I disable the autocomplete, it catches the Enter key)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
KeyDown 和 KeyPress 之间的区别
在你的情况下最好您可以做的就是使用 KeyDown 事件。
关于 KeyPress 事件的另一个有趣的事情是:如果组合框没有项目,它甚至会捕获带有自动竞争的 Enter 键! :-)
Difference between KeyDown and KeyPress
In your case the best you may do is use KeyDown event.
Another interesting thing about KeyPress event is: it even catches Enter key with autocompete on if the combobox has no items! :-)