簡體   English   中英

使用TextBox C#中的任何字符自動完成

[英]Autocomplete with any character in TextBox C#

我想使用LINQ實體將自動完成設置為文本框。

這是我的代碼:

using (Reference_TraductionEntities context = new Reference_TraductionEntities())
{
    var source = new AutoCompleteStringCollection();

    var name = from a in context.Feuil1Prenom
               where a.PRENOMF.StartsWith("i")
               select a.PRENOMF;
    source.AddRange(name.ToArray());

    textBox1.AutoCompleteCustomSource = source;
    textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
}

這段代碼還可以,但是只要有字符“ i”,我就希望文本框中的任何字符輸入都自動完成

我該如何解決?

謝謝,

試試這個

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;
     }
  }
}

非常感謝BugFinder !!!!

我替換以“ i”開頭以textbox1.text開頭...

using (Reference_TraductionEntities context = new Reference_TraductionEntities())
{
    var source = new AutoCompleteStringCollection();
    var name = from a in context.Feuil1Prenom
               where a.PRENOMF.StartsWith(textBox1.Text )
               select a.PRENOMF;
    source.AddRange(name.ToArray());

    textBox1.AutoCompleteCustomSource = source;
    textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
}

有用 !!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM