从自动完成建议列表中选择一个项目会使用 ENTER 键引发 KeyDown 事件 [英] Selecting an item from AutoComplete suggestion list raises KeyDown-event with ENTER key

查看:16
本文介绍了从自动完成建议列表中选择一个项目会使用 ENTER 键引发 KeyDown 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Winforms 中,我有一个文本框,其中 AutoCompleteMode 设置为 SuggestAppend,并设置了 AutoCompleteCustomSource.当用户键入一些字母时,会显示建议列表.如果通过用鼠标单击来选择此列表中的一个项目,则为 ENTER 键引发包含文本框的表单的 KeyDown 事件.

In Winforms I have a textbox with AutoCompleteMode set to SuggestAppend and a AutoCompleteCustomSource set. 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.

是否有可能在用鼠标选择建议的项目时不引发此事件?

Is there any possibility to NOT raise this event when selecting a suggested item with the mouse?

推荐答案

AutoComplete 功能有几个怪癖,它们继承自其最初设计的用途,即 Internet Explorer 的地址框.这包括当您单击列表中的项目时发出 Enter 键.在 IE 的地址框中按 Enter 可以导航到输入的 URL.

The AutoComplete feature has a couple of quirks that were inherited from its original designed use, the address box of Internet Explorer. This includes emitting the Enter key when you click on an item in the list. Pressing Enter in the address box of IE makes it navigate to the entered URL.

对此您无能为力,本机界面 (IAutoComplete2) 几乎没有配置其工作方式的选项.它通过伪造 Windows 消息将击键插入文本框中.这是您可以区分差异的一种方式,实际的键不会被按下.您可以通过调用 GetKeyState() 进行检查,如下所示:

There isn't anything you can do about that, the native interface (IAutoComplete2) has very few options to configure the way it works. It pokes the keystrokes into the text box by faking Windows messages. 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:

    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);

这篇关于从自动完成建议列表中选择一个项目会使用 ENTER 键引发 KeyDown 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆