从自动完成建议列表中选择一个项目会引发带有 ENTER 键的 KeyDown-event

Selecting an item from AutoComplete suggestion list raises KeyDown-event with ENTER key(从自动完成建议列表中选择一个项目会引发带有 ENTER 键的 KeyDown-event)
本文介绍了从自动完成建议列表中选择一个项目会引发带有 ENTER 键的 KeyDown-event的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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?

推荐答案

自动完成功能有几个怪癖,这些怪癖继承自其最初的设计用途,即 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-event的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)