如何让自动完成不仅搜索开头,还使用包含,例如包含 "Doors"

How to Get Autocomplete to not just search the starts with but uses contains, for eg contains "Doors"

使用自动完成功能,您可以搜索开头,这非常有效。但是,搜索的字符串并不总是以您要查找的字符串开头。例如,从我添加的包含字符串的字典中...

开门元素
外门
室内门
推拉门

仅从自动完成搜索返回门打开元素。

这是我用来填充自动完成的代码...

        Dictionary<string, string> items = KN_File.CreateDictFromTextFile(filePath);
        List<string> treeBasedList = new List<string>();

        foreach (KeyValuePair<string, string> kvp in items)
        {
            treeBasedList.Add(kvp.Value + "," + kvp.Key);
        }

        AutoCompleteStringCollection AutoCompleteList = new AutoCompleteStringCollection();
        AutoCompleteList.AddRange(treeBasedList.ToArray());
        comboBx.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        comboBx.AutoCompleteSource = AutoCompleteSource.CustomSource;
        comboBx.AutoCompleteCustomSource = AutoCompleteList;

这是一个解决方法,也许您可​​以参考。

private void Form1_Load(object sender, EventArgs e)
{
    listOriginal.Add("Doors opening elements");
    listOriginal.Add("exterior doors");
    listOriginal.Add("interior doors");
    listOriginal.Add("sliding doors");

    this.comboBx.Items.AddRange(listOriginal.ToArray());
}

// Bind default keywords
List<string> listOriginal = new List<string>();
// save new keywords
List<string> listNew = new List<string>();

private void comboBx_TextUpdate(object sender, EventArgs e)
{
    //clear combobox
    this.comboBx.Items.Clear();
    //clear listNew
    listNew.Clear();
    foreach (var item in listOriginal)
    {
        // call ToLower() .. not case sensitive
        if (item.ToLower().Contains(this.comboBx.Text))
        {
            //add to ListNew
            listNew.Add(item);
        }
    }
    this.comboBx.Items.AddRange(listNew.ToArray());
    this.comboBx.SelectionStart = this.comboBx.Text.Length;
    Cursor = Cursors.Default;
    // Automatically pop up drop-down
    this.comboBx.DroppedDown = true;
}

测试结果,