自动完成组合框onkey preSS事件吃掉Enter键组合、自动完成、事件、onkey

2023-09-04 22:41:11 作者:黑暗中的影子。

我有一个 AutoCompleteMode =组合框提示和处理关键preSS事件像这样:

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.

我也试过这里提供的建议:http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/2db0b540-756a-4a4f-9371-adbb92409806,设置窗体的键preVIEW 属性为true,把一个断点形式的关键preSS事件处理程序:

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!

有什么建议?

(如果我禁用了自动完成,它捕获回车键)

(If I disable the autocomplete, it catches the Enter key)

推荐答案

Difference KeyDown和关键preSS 之间

在你的情况,你可以做的最好的是使用KeyDown事件。

In your case the best you may do is use KeyDown event.

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

有关重点preSS事件另一个有趣的事情是:它甚至渔获物。如果组合框有没有项目Enter键与autocompete! :-)

Another interesting thing about KeyPress event is: it even catches Enter key with autocompete on if the combobox has no items! :-)