
将下列代码添加到窗口加载函数中即可。假设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;
您可能感兴趣的文章:C# ComboBox控件“设置 DataSource 属性后无法修改项集合”的完美解决方法C#实现带搜索功能的ComboBoxC# 重写ComboBox实现下拉任意组件的方法C# ComboBox的联动 *** 作(三层架构)C#实现ComboBox控件显示出多个数据源属性的方法C#实现绑定Combobox的方法C#用ComboBox控件实现省与市的联动效果的方法C#(WinForm) ComboBox和ListBox添加项及设置默认选择项C# listview添加combobox到单元格的实现代码c#构造ColorComboBox(颜色下拉框)C#中comboBox实现三级联动 总结
以上是内存溢出为你收集整理的C#实现ComboBox自动匹配字符全部内容,希望文章能够帮你解决C#实现ComboBox自动匹配字符所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)