C#中的组合框和自动完成 [英] Combobox and autocomplete in C#
问题描述
我对组合框中的自动完成选项有一个小问题.一切正常,除了我想以不同的方式工作:)
i have small problem with autocomplete option in combobox. Everything is working correct, except that i want to work it diffrent :)
当我开始在组合框中输入时,自动建议按我喜欢的方式工作:
When I start typing in combobox, autusuggest working the way i like :
但是当我第一次打开组合框,然后开始输入时,我会得到类似的东西:
But when i first open combobox, and then start typing i get something like that:
此外,我无法从自动建议组合框中选择项目,只能从下面的列表中选择.
What's more i can't pick item from autosuggest combobox, only from this list under.
AutocompleteMode 是 SuggestAppend
AutocompleteMode is SuggestAppend
我想在第一张图片上有自动建议,在图片 2 的情况下,第一个组合框列表应该以某种方式关闭..
I'd like to have autosuggest like on the first picture, and in situations like picture 2, this first combobox list should be closed somehow..
推荐答案
我遇到了同样的问题,是这样解决的:
I had the same problem and solved it this way:
private void comboBox_DropDown(object sender, EventArgs e)
{
ComboBox cbo = (ComboBox)sender;
cbo.PreviewKeyDown += new PreviewKeyDownEventHandler(comboBox_PreviewKeyDown);
}
private void comboBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
ComboBox cbo = (ComboBox)sender;
cbo.PreviewKeyDown -= comboBox_PreviewKeyDown;
if (cbo.DroppedDown) cbo.Focus();
}
一旦用户单击DropDown
按钮,PreviewKeyDown
事件就会附加到该ComboBox
.当用户开始输入时,会触发新添加的事件.在这种情况下,我们检查 ComboBox
是否为 DroppedDown
,如果是,则关注 ComboBox
.在 ComboBox
焦点 DropDown
消失,就是这样.
Once the user clicks on the DropDown
button PreviewKeyDown
event is attached to that ComboBox
. When user starts typing, freshly added event is triggered. In that event we check if ComboBox
is DroppedDown
, if it is, focus that ComboBox
. On ComboBox
focus DropDown
disappeares and that's it.
这篇关于C#中的组合框和自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!