简体   繁体   English

Combobox onkeypress事件的自动完成功能会占用Enter键

[英]Autocomplete on Combobox onkeypress event eats up the Enter key

I have a ComboBox with AutoCompleteMode = suggest and handle the KeyPress event like so: 我有一个ComboBox与AutoCompleteMode = suggest并处理KeyPress事件,如下所示:

private void searchBox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Return)
    {
        // do stuff
    }
}

However, it does not catch the Enter key. 但是,它没有捕获Enter键。 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: 我也尝试了这里提供的建议: 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;
}

However, even the form's handler was not catching the enter key! 但是,即使表单的处理程序没有捕获回车键!

Any suggestions? 有什么建议?

(If I disable the autocomplete, it catches the Enter key) (如果我禁用自动完成功能,它会捕获Enter键)

Difference between KeyDown and KeyPress KeyDown和KeyPress之间的区别

In your case the best you may do is use KeyDown event. 在您的情况下,您可能做的最好的事情是使用KeyDown事件。

void SearchBox_KeyDown(object sender, KeyEventArgs e)
{
   if(e.KeyCode == Keys.Enter)
    {
        // Do stuff
    }
}

Another interesting thing about KeyPress event is: it even catches Enter key with autocompete on if the combobox has no items! 关于KeyPress事件的另一个有趣的事情是:如果组合框没有项目,它甚至可以使用autocompete捕获Enter键! :-) :-)

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

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