小编典典

自动完成文本框控件

c#

我想要一个文本框控件,该控件建议并使用C#2008和LINQ从Windows应用程序中的数据库添加值。

我用组合框来做,但是不能用文本框来做。

我该怎么做?


阅读 290

收藏
2020-05-19

共1个答案

小编典典

这可能不是最好的处理方式,但应该可以:

 this.textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
 this.textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;

private void textBox1_TextChanged(object sender, EventArgs e)
{
    TextBox t = sender as TextBox;
    if (t != null)
    {
        //say you want to do a search when user types 3 or more chars
        if (t.Text.Length >= 3)
        {
            //SuggestStrings will have the logic to return array of strings either from cache/db
            string[] arr = SuggestStrings(t.Text);

            AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
            collection.AddRange(arr);

            this.textBox1.AutoCompleteCustomSource = collection;
        }
    }
}
2020-05-19