简体   繁体   中英

Autocomplete with any character in TextBox C#

I want to set the autocomplete to the textbox using LINQ to entities.

This is my code:

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

This code is OK , but just with the character "i", I want autocomplete with any character entry in textbox

How can I fix it ?

Thanks,

Just try this one

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

Very Thanks BugFinder !!!!

I replace starts with "i" to starts with 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;
}

It works !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM