问题描述
在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!