c#仅使用部分输入文本自动完成文本框

c#仅使用部分输入文本自动完成文本框,c#,autocomplete,C#,Autocomplete,我想只使用用户输入的部分文本在文本框中实现自动完成功能。例如,如果用户输入“123 abc”,我希望autocomplete只处理“abc”部分 我尝试过用数据填充哈希集(超过500000项)。然后使用regex@“^[\d]*\s*”成功地删除字符串开头的数字和空格,这样我只剩下“abc”(但我想将数字保留在文本框中)),然后通过迭代哈希集中的每个值,使用value.StartsWith(“abc”)填充列表。最后,我将所有过滤项目添加到组合框项目中 这个方法非常慢,给我最多5秒或更长的暂停时

我想只使用用户输入的部分文本在文本框中实现自动完成功能。例如,如果用户输入“123 abc”,我希望autocomplete只处理“abc”部分

我尝试过用数据填充哈希集(超过500000项)。然后使用regex
@“^[\d]*\s*”
成功地删除字符串开头的数字和空格,这样我只剩下“abc”(但我想将数字保留在文本框中)),然后通过迭代哈希集中的每个值,使用
value.StartsWith(“abc”)
填充列表。最后,我将所有过滤项目添加到组合框项目中


这个方法非常慢,给我最多5秒或更长的暂停时间,文本框自动完成方法更快,实现得更好。我可以只使用字符串的一部分来使用文本框自动完成吗?或者您可以建议使用其他同样快速的实现吗?

您是否尝试过/考虑过二进制搜索方法?它可以快得多

您只需将它们全部添加到一个列表中,对其进行排序,然后在搜索中使用,如下代码所示

另一种方法是将其存储在一个索引数据库中,使用文本索引将非常快

例如

如果未找到精确匹配项,则List.BinarySearch将返回比请求大的下一项索引的补数

//So, you can do it like this (assuming you'll never get an exact match):

var key = (cardName + Config.EditionShortToLong(edition)).ToLower();
var list = CardBase.cardList;

var index = ~list.BinarySearch(key);
return index != list.Count && list[index].StartsWith(key);
祝你好运

使用系统;
使用System.Collections.Generic;
公开课范例
{
公共静态void Main()
{
列出恐龙=新列表();
添加(“厚头龙”);
恐龙。添加(“Amargasaurus”);
恐龙。添加(“马门基龙”);
恐龙。添加(“Deinonychus”);
Console.WriteLine();
foreach(恐龙中的字符串恐龙)
{
控制台。书写线(恐龙);
}
Console.WriteLine(“\nSort”);
恐龙;
Console.WriteLine();
foreach(恐龙中的字符串恐龙)
{
控制台。书写线(恐龙);
}
Console.WriteLine(“\n二进制搜索并插入\“腔突\\”:”);
int index=恐龙。二元搜索(“腔突”);
如果(指数<0)
{
恐龙.插入(~索引,“腔突”);
}
Console.WriteLine();
foreach(恐龙中的字符串恐龙)
{
控制台。书写线(恐龙);
}
Console.WriteLine(“\n搜索并插入\“暴龙\:”);
索引=恐龙。二元搜索(“暴龙”);
如果(指数<0)
{
恐龙.插入(~索引,“暴龙”);
}
Console.WriteLine();
foreach(恐龙中的字符串恐龙)
{
控制台。书写线(恐龙);
}
}
}
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();

        dinosaurs.Add("Pachycephalosaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Mamenchisaurus");
        dinosaurs.Add("Deinonychus");

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nSort");
        dinosaurs.Sort();

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nBinarySearch and Insert \"Coelophysis\":");
        int index = dinosaurs.BinarySearch("Coelophysis");
        if (index < 0)
        {
            dinosaurs.Insert(~index, "Coelophysis");
        }

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nBinarySearch and Insert \"Tyrannosaurus\":");
        index = dinosaurs.BinarySearch("Tyrannosaurus");
        if (index < 0)
        {
            dinosaurs.Insert(~index, "Tyrannosaurus");
        }

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }
    }
}