2016-08-15 18 views
0

エンティティにLINQを使用してテキストボックスにオートコンプリートを設定したいとします。TextBoxの任意の文字をオートコンプリートC#

これは私のコードです:

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

このコードはOKですが、単に文字「I」、 と私はそれを修正することができますどのようにテキストボックス

に任意の文字入力を自動補完したいですか?

おかげで、

+1

次に「textbox1.text」で始まる場合は「i」で始まります。 – BugFinder

+1

場所を削除するとどうなりますか? – ntohl

+0

私はどこで削除する場合、私はエラーが表示されます:vshost32.exeは動作を停止! – devtunis

答えて

0

を試してみてください!!!!

私はそれが動作

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

... textbox1.textで始まりに "I" で始まるを交換してください!

0

ただ非常に感謝BugFinderこの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; 
    } 
    } 
} 
関連する問題