91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 8KB
    文件類型: .cs
    金幣: 1
    下載: 0 次
    發布日期: 2021-06-07
  • 語言: C#
  • 標簽: ComboBox??

資源簡介

C#WinForm的ComboBox控件自定義實現自動模糊匹配查找數據的方法 與控件自帶的AutoCompleteMode類似,完美實現模糊匹配,解決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();
????????}

????????//初始化綁定默認關鍵詞(此數據源可以從數據庫取)
????????List?listOnit?=?new?List();
????????//輸入key之后,返回的關鍵詞
????????List?listNew?=?new?List();

????????//當前輸入框內的信息變量(只有在輸入框里輸入信息是此變量才會有值其他時候為默認空值)
????????string?CbxNowStr?=?““;
????????//輸入框內的前一個歷史信息變量
????????string?CbxOldStr?=?““;

????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????//檢測默認數組內是否有值
????????????if?(listOnit.Count?>?0)
????????????{
????????????????//清空默認數組
????????????????listOnit.Clear();
????????????}
????????????//向數組內添加數據
????????????listOnit.Add(“張三“);
????????????listOnit.Add(“張四“);
????????????listOnit.Add(“張五“);
????????????listOnit.Add(“李三“);
????????????listOnit.Add(“李四“);
????????????listOnit.Add(“李五“);
????????????listOnit.Add(“王三“);
????????????listOnit.Add(“王四“);
????????????listOnit.Add(“王五“);
????????????listOnit.Add(“三哥“);
????????????listOnit.Add(“四哥“);
????????????listOnit.Add(“五哥“);
????????????//以上事例為測試數據
????????????/*
?????????????*?1.注意用Item.Add(obj)或者Item.AddRange(obj)方式添加
?????????????*?2.如果用DataSource綁定,后面再進行綁定是不行的,即便是Add或者Clear也不行
?????????????*/

????????????//調用數據綁定的方法
????????????BindComboBox();
????????????
????????????//以下兩行為控件默認的方法只能從左向右匹配且無法和自己定義的查詢方式同時使用
????????????//this.comboBox7.AutoCompleteSource?=?AutoCompleteSource.ListItems;
????????????//this.comboBox7.AutoCompleteMode?=?AutoCompleteMode.Suggest;
????????}

????????///?
????????///?動態綁定ComboBox數據的方法
????????///?

????????private?void?BindComboBox()
????????{
????????????//檢測臨時數組是否有值.初始化時臨時數組為空
????????????if?(listNew.Count?>?0)
????????????{
????????????????//清空COMBOBOX里的下拉框數據
????????????????if?(this.comboBox7.Items.Count?>?0)
????????????????{
????????????????????this.comboBox7.Items.Clear();
????????????????}
????????????????//重新綁定新數據
????????????????this.comboBox7.Items.AddRange(listNew.ToArray());//綁定數據
????????????????//指定下拉框顯示項長度
????????????????this.comboBox7.MaxDropDownItems?=?listNew.Count;
????????????????//清空臨時數組
????????????????this.listNew.Clear();
????????????}
????????????//默認數組內有值且當前輸入框內容為空
????????????else?if?(listOnit.Count?>?0?&&?CbxNowStr?==?““)
????????????{
????????????????//清空COMBOBOX里的下拉框數據
????????????????if?(this.comboBox7.Items.Count?>?0)
????????????????{
????????????????????this.comboBox7.Items.Clear();
????????????????}
????????????????//綁定默認數組數據給下拉框
????????????????this.comboBox7.Items.AddRange(listOnit.ToArray());//綁定數據
????????????????//指定下拉框顯示項長度
????????????????this.comboBox7.MaxDropDownItems?=?listOnit.Count;
????????????}
????????}
????????//此方法內會用到DroppedDown方法所以必須使用T

評論

共有 條評論