简体   繁体   English

从AutoComplete建议列表中选择项目会使用ENTER键引发KeyDown事件

[英]Selecting an item from AutoComplete suggestion list raises KeyDown-event with ENTER key

In Winforms I have a textbox with AutoCompleteMode set to SuggestAppend and a AutoCompleteCustomSource set. 在Winforms中,我有一个文本框,其中AutoCompleteMode设置为SuggestAppend,并设置了AutoCompleteCustomSource。 When the user types some letters the suggestion list is shown. 当用户键入一些字母时,显示建议列表。 If an item of this list is selected by clicking it with the mouse, the KeyDown-event of the form containing the textbox is raised for the ENTER key. 如果通过鼠标单击选择此列表中的项目,则会为ENTER键引发包含文本框的表单的KeyDown事件。

Is there any possibility to NOT raise this event when selecting a suggested item with the mouse? 使用鼠标选择建议项目时是否有可能不引发此事件?

The AutoComplete feature has a couple of quirks that were inherited from its original designed use, the address box of Internet Explorer. 自动完成功能有一些从其原始设计用途(Internet Explorer的地址框)继承的怪癖。 This includes emitting the Enter key when you click on an item in the list. 这包括单击列表中的项目时发出Enter键。 Pressing Enter in the address box of IE makes it navigate to the entered URL. 在IE的地址栏中按Enter键可以导航到输入的URL。

There isn't anything you can do about that, the native interface (IAutoComplete2) has very few options to configure the way it works. 你无能为力,原生界面(IAutoComplete2)只有很少的选项来配置它的工作方式。 It pokes the keystrokes into the text box by faking Windows messages. 它通过伪造Windows消息将键击戳到文本框中。 Which is one way you can tell the difference, the actual key won't be down. 哪种方式可以区分,实际的密钥不会下降。 Something you can check by pinvoking GetKeyState(), like this: 你可以通过pinkeoking GetKeyState()检查一下,像这样:

    private void textBox1_KeyDown(object sender, KeyEventArgs e) {
        if (e.KeyData == Keys.Enter && GetKeyState(Keys.Enter) < 0) {
            Console.WriteLine("Really down");
        }
    }

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern short GetKeyState(Keys key);

You can catch keydown keys: 你可以抓住keydown键:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
    //Do nothing or something
    }
}

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

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