模糊搜寻- Winforms ComboBox

先假设模糊搜寻功能要运用在我的英文单字本小专案上:

制作此专案一开始是使用TextBox的AutoComplete功能,在搜寻上只能抓首字母,
但由于我一直想实现相似于我们在网路搜寻引擎,那样的关键字模糊搜寻,现在终于找到解方了!
只要输入的关键字(intput string),哪些单字有含这些字母,就会列在ComboBox的items里面,接着来直接看示例吧!
模糊搜寻- Winforms ComboBox
程式逻辑思维步骤:
1.在Form形成物件时,就对ComboBox的items collection,以programming的方式,加入所有单字的集合(从List<string> beforeSelected)。

2.在用户输入关键字搜寻后,以programming的方式,先清空ComboBox的items collection。此关键字成为筛选条件,将beforeSelected内的元素全部比对一遍,英文单字含有关键字的就加入新的集合(List<string> afterSelected)。----搭配ComboBox的TextUpdate事件。

3.再将ComboBox的items collection,以programming的方式,加入筛选后的集合(从List<string> afterSelected)。

4.ComboBox加入items后,使用DroppedDown=true,这时已经可以看到符合关键字的所有单字!

程式码:


    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

            string configPath=$@"C:\Users\ching\source\repos\A20200615\Vocabulary";

            /* 步驟1 */
            Form1.SetAutoCompleteComboBox(configPath, comboBox_Search);

        } 

   public static void SetAutoCompleteComboBox(string configPath, ComboBox comboBox)
        {
            /* 放所有單字的集合 */
            List<string> beforeSelected = new List<string>();
            /* 篩選後符合關鍵字的集合 */
            List<string> afterSelected = new List<string>();

            /* 得到此路徑下面的所有目錄=>單字資料夾 */
            string[] directories = Directory.GetDirectories(configPath);

            foreach (var directory in directories)
            {
                DirectoryInfo folder = new DirectoryInfo(directory);

                /* 把資料夾名稱一一存入篩選前集合 */
                beforeSelected.Add(folder.Name.ToLower());                 
            }

            /* 步驟2 */
            /* 清空comboBox items collection準備換成新的list datasource */
            if (comboBox.Items.Count!=0)
            {
                comboBox.Items.Clear();
            }

            /* 儲存用戶輸入的字元 */
            var input = comboBox.Text.ToLower();

            /* Form1物件形成時,或用戶刪除重新輸入關鍵字時,會進入此判斷式 */
            /* ComboBox搜尋框無字元 */
            if (string.IsNullOrEmpty(input))
            {    
                /* comboBox items collection加入篩選前集合 */
                comboBox.Items.AddRange(beforeSelected.ToArray());

            }
            else
            {
                /* 步驟2 */
                /* 篩選符合關鍵字的單字,加入新的集合 */

                foreach (var item in beforeSelected)
                {
                    if (item.IndexOf(input) >=0)
                    {
                        afterSelected.Add(item);
                    }
                }

                 /* comboBox items collection加入篩選後集合 */
                comboBox.Items.AddRange(afterSelected.ToArray());

                /* 這個Select方法解決了輸入游標不斷置回起始位置的問題 */
                comboBox.Select(comboBox.Text.Length, 0);
                comboBox.DroppedDown = true;

                /* 使用DroppedDown之後,鼠標箭頭消失在comboBox上,設定取得預設游標(箭號游標) */
                comboBox.Cursor = Cursors.Default;

            }

        }

        /* 步驟2搭配ComboBox的 TextUpdate事件 */
        private void comboBox_Search_TextUpdate(object sender, EventArgs e)
        {
            Form1.SetAutoCompleteComboBox(configPath, comboBox_Search);
        }

    }      

如有叙述错误,还请不吝啬留言指教,thanks!

猜你喜欢

转载自blog.51cto.com/14890430/2517149