資源簡介
C#WinForm的ComboBox控件自定義實(shí)現(xiàn)自動模糊匹配查找數(shù)據(jù)的方法
與控件自帶的AutoCompleteMode類似,完美實(shí)現(xiàn)模糊匹配,解決AutoCompleteMode只能從左向右匹配的問題
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
namespace?test
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????//初始化綁定默認(rèn)關(guān)鍵詞(此數(shù)據(jù)源可以從數(shù)據(jù)庫取)
????????List?listOnit?=?new?List();
????????//輸入key之后,返回的關(guān)鍵詞
????????List?listNew?=?new?List();
????????//當(dāng)前輸入框內(nèi)的信息變量(只有在輸入框里輸入信息是此變量才會有值其他時(shí)候?yàn)槟J(rèn)空值)
????????string?CbxNowStr?=?““;
????????//輸入框內(nèi)的前一個(gè)歷史信息變量
????????string?CbxOldStr?=?““;
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????//檢測默認(rèn)數(shù)組內(nèi)是否有值
????????????if?(listOnit.Count?>?0)
????????????{
????????????????//清空默認(rèn)數(shù)組
????????????????listOnit.Clear();
????????????}
????????????//向數(shù)組內(nèi)添加數(shù)據(jù)
????????????listOnit.Add(“張三“);
????????????listOnit.Add(“張四“);
????????????listOnit.Add(“張五“);
????????????listOnit.Add(“李三“);
????????????listOnit.Add(“李四“);
????????????listOnit.Add(“李五“);
????????????listOnit.Add(“王三“);
????????????listOnit.Add(“王四“);
????????????listOnit.Add(“王五“);
????????????listOnit.Add(“三哥“);
????????????listOnit.Add(“四哥“);
????????????listOnit.Add(“五哥“);
????????????//以上事例為測試數(shù)據(jù)
????????????/*
?????????????*?1.注意用Item.Add(obj)或者Item.AddRange(obj)方式添加
?????????????*?2.如果用DataSource綁定,后面再進(jìn)行綁定是不行的,即便是Add或者Clear也不行
?????????????*/
????????????//調(diào)用數(shù)據(jù)綁定的方法
????????????BindComboBox();
????????????
????????????//以下兩行為控件默認(rèn)的方法只能從左向右匹配且無法和自己定義的查詢方式同時(shí)使用
????????????//this.comboBox7.AutoCompleteSource?=?AutoCompleteSource.ListItems;
????????????//this.comboBox7.AutoCompleteMode?=?AutoCompleteMode.Suggest;
????????}
????????///?
????????///?動態(tài)綁定ComboBox數(shù)據(jù)的方法
????????///?
????????private?void?BindComboBox()
????????{
????????????//檢測臨時(shí)數(shù)組是否有值.初始化時(shí)臨時(shí)數(shù)組為空
????????????if?(listNew.Count?>?0)
????????????{
????????????????//清空COMBOBOX里的下拉框數(shù)據(jù)
????????????????if?(this.comboBox7.Items.Count?>?0)
????????????????{
????????????????????this.comboBox7.Items.Clear();
????????????????}
????????????????//重新綁定新數(shù)據(jù)
????????????????this.comboBox7.Items.AddRange(listNew.ToArray());//綁定數(shù)據(jù)
????????????????//指定下拉框顯示項(xiàng)長度
????????????????this.comboBox7.MaxDropDownItems?=?listNew.Count;
????????????????//清空臨時(shí)數(shù)組
????????????????this.listNew.Clear();
????????????}
????????????//默認(rèn)數(shù)組內(nèi)有值且當(dāng)前輸入框內(nèi)容為空
????????????else?if?(listOnit.Count?>?0?&&?CbxNowStr?==?““)
????????????{
????????????????//清空COMBOBOX里的下拉框數(shù)據(jù)
????????????????if?(this.comboBox7.Items.Count?>?0)
????????????????{
????????????????????this.comboBox7.Items.Clear();
????????????????}
????????????????//綁定默認(rèn)數(shù)組數(shù)據(jù)給下拉框
????????????????this.comboBox7.Items.AddRange(listOnit.ToArray());//綁定數(shù)據(jù)
????????????????//指定下拉框顯示項(xiàng)長度
????????????????this.comboBox7.MaxDropDownItems?=?listOnit.Count;
????????????}
????????}
????????//此方法內(nèi)會用到DroppedDown方法所以必須使用T
評論
共有 條評論