C#實(shí)現(xiàn)ComboBox自動(dòng)匹配字符
更新時(shí)間:2012年12月19日 09:06:25 作者:
本文介紹C#如何實(shí)現(xiàn)ComboBox自動(dòng)匹配字符1.采用CustomSource當(dāng)做提示集合2. 直接使用下拉列表中的項(xiàng)作為匹配的集合,需要了解的朋友可以參考下
1. 采用CustomSource當(dāng)做提示集合
將下列代碼添加到窗口加載函數(shù)中即可。假設(shè)unitNameList是獲取的想要添加到下拉列表中的字符串列表。
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
// 獲取單位列表
List<string> unitNameList = this.getAllUnitName();
foreach (string unitname in unitNameList)
{
collection.Add(unitname);
//Console.WriteLine("自動(dòng)提示" + unitname);
}
this.comboBox2.AutoCompleteCustomSource = collection;
this.comboBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
this.comboBox2.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
其中AutoCompleteMode包含None,Suggest,Append和SuggestAppend四種情況。
None:關(guān)閉自動(dòng)補(bǔ)全功能
Suggest:展開下拉列表并顯示匹配的結(jié)果
Append:自動(dòng)補(bǔ)全
SuggestAppend:Suggest和Append的組合,即顯示下拉列表也自動(dòng)補(bǔ)全。
2. 直接使用下拉列表中的項(xiàng)作為匹配的集合
AutoCompleteSource設(shè)置為L(zhǎng)istItems。
// 獲取單位列表
List<string> unitNameList = this.getAllUnitName();
foreach (string unitname in unitNameList)
{
this.comboBox2.Items.Add(unitname);
}
this.comboBox2.AutoCompleteSource = AutoCompleteSource.ListItems;
將下列代碼添加到窗口加載函數(shù)中即可。假設(shè)unitNameList是獲取的想要添加到下拉列表中的字符串列表。
復(fù)制代碼 代碼如下:
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
// 獲取單位列表
List<string> unitNameList = this.getAllUnitName();
foreach (string unitname in unitNameList)
{
collection.Add(unitname);
//Console.WriteLine("自動(dòng)提示" + unitname);
}
this.comboBox2.AutoCompleteCustomSource = collection;
this.comboBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
this.comboBox2.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
其中AutoCompleteMode包含None,Suggest,Append和SuggestAppend四種情況。
None:關(guān)閉自動(dòng)補(bǔ)全功能
Suggest:展開下拉列表并顯示匹配的結(jié)果
Append:自動(dòng)補(bǔ)全
SuggestAppend:Suggest和Append的組合,即顯示下拉列表也自動(dòng)補(bǔ)全。
2. 直接使用下拉列表中的項(xiàng)作為匹配的集合
AutoCompleteSource設(shè)置為L(zhǎng)istItems。
復(fù)制代碼 代碼如下:
// 獲取單位列表
List<string> unitNameList = this.getAllUnitName();
foreach (string unitname in unitNameList)
{
this.comboBox2.Items.Add(unitname);
}
this.comboBox2.AutoCompleteSource = AutoCompleteSource.ListItems;
您可能感興趣的文章:
- C# ComboBox控件“設(shè)置 DataSource 屬性后無法修改項(xiàng)集合”的完美解決方法
- C#實(shí)現(xiàn)帶搜索功能的ComboBox
- C# 重寫ComboBox實(shí)現(xiàn)下拉任意組件的方法
- C# ComboBox的聯(lián)動(dòng)操作(三層架構(gòu))
- C#實(shí)現(xiàn)ComboBox控件顯示出多個(gè)數(shù)據(jù)源屬性的方法
- C#實(shí)現(xiàn)綁定Combobox的方法
- C#用ComboBox控件實(shí)現(xiàn)省與市的聯(lián)動(dòng)效果的方法
- C#(WinForm) ComboBox和ListBox添加項(xiàng)及設(shè)置默認(rèn)選擇項(xiàng)
- C# listview添加combobox到單元格的實(shí)現(xiàn)代碼
- c#構(gòu)造ColorComboBox(顏色下拉框)
- C#中comboBox實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)
相關(guān)文章
C#實(shí)現(xiàn)獲取IIS站點(diǎn)及虛擬目錄信息的方法
這篇文章主要介紹了C#實(shí)現(xiàn)獲取IIS站點(diǎn)及虛擬目錄信息的方法,可實(shí)現(xiàn)獲取IIS站點(diǎn)信息及物理路徑等功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
2015-10-10
C#使用HtmlAgilityPack實(shí)現(xiàn)解析提取HTML內(nèi)容
HtmlAgilityPack是一個(gè)HTML解析類庫(kù),這篇文章主要為大家詳細(xì)介紹了C#如何使用HtmlAgilityPack實(shí)現(xiàn)解析提取HTML內(nèi)容,感興趣的小伙伴可以參考一下
2023-12-12
C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源
本文主要介紹了C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
2023-02-02
Unity UGUI的ScrollRect滾動(dòng)視圖組件使用詳解
這篇文章主要為大家介紹了Unity UGUI的ScrollRect滾動(dòng)視圖組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
2023-07-07 
