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# RGB圖像和灰度圖像互轉(zhuǎn)的實(shí)現(xiàn)
在我們的圖像類型教程中定義了RGB顏色模型和灰度格式,本文主要介紹了C# RGB圖像和灰度圖像互轉(zhuǎn)的實(shí)現(xiàn),文中通過代碼介紹的非常清楚,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08C#中LINQ多條件JOIN時(shí)為什么可以使用匿名類
這篇文章主要給大家介紹了關(guān)于C#中LINQ多條件JOIN時(shí)為什么可以使用匿名類的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧2018-09-09WinForm實(shí)現(xiàn)移除控件某個(gè)事件的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)移除控件某個(gè)事件的方法,對(duì)C#初學(xué)者有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08C#設(shè)置自定義文件圖標(biāo)實(shí)現(xiàn)雙擊啟動(dòng)(修改注冊(cè)表)
這篇文章介紹的是利用C#設(shè)置自定義文件圖標(biāo),然后實(shí)現(xiàn)雙擊啟動(dòng)的功能,文章給出了示例代碼,介紹的很詳細(xì),有需要的可以參考借鑒。2016-08-08