当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现ComboBox自动匹配字符

C#实现ComboBox自动匹配字符

2019年07月18日  | 移动技术网IT编程  | 我要评论
1. 采用customsource当做提示集合 将下列代码添加到窗口加载函数中即可。假设unitnamelist是获取的想要添加到下拉列表中的字符串列表。 复制代码 代码如
1. 采用customsource当做提示集合
将下列代码添加到窗口加载函数中即可。假设unitnamelist是获取的想要添加到下拉列表中的字符串列表。
复制代码 代码如下:

autocompletestringcollection collection = new autocompletestringcollection();
// 获取单位列表
list<string> unitnamelist = this.getallunitname();
foreach (string unitname in unitnamelist)
{
collection.add(unitname);
//console.writeline("自动提示" + unitname);
}
this.combobox2.autocompletecustomsource = collection;
this.combobox2.autocompletesource = autocompletesource.customsource;
this.combobox2.autocompletemode = autocompletemode.suggestappend;

其中autocompletemode包含none,suggest,append和suggestappend四种情况。
none:关闭自动补全功能
suggest:展开下拉列表并显示匹配的结果
append:自动补全
suggestappend:suggest和append的组合,即显示下拉列表也自动补全。

2. 直接使用下拉列表中的项作为匹配的集合
autocompletesource设置为listitems。
复制代码 代码如下:

// 获取单位列表
list<string> unitnamelist = this.getallunitname();
foreach (string unitname in unitnamelist)
{
this.combobox2.items.add(unitname);
}
this.combobox2.autocompletesource = autocompletesource.listitems;

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网