2017-08-28 180 views
0

我有用於輸入字符串和顯示在這個textbox.The當前代碼的所有繳費結果的文本框等如下:C#文本框搜索自動完成

private void Form_Load(object sender, EventArgs e) 
{ 
    TextBox.AutoCompleteMode = AutoCompleteMode.Suggest; 
    TextBox.AutoCompleteSource = AutoCompeteSource.CustomSource; 
} 

private void TextBox_TextChanged(object sender, EventArgs e) 
{ 
    TextBox t = sender as TextBox; 
    if(t != null) 
    { 
     if(t.Text.Length > = 1) 
     { 
      AutoCompleteStringCollection collection = new AutoCompleteStringCollection(); 
      collection.AddRange(s.Name); 
      this.TextBox.AutoCompleteCustomSource = collection; 
     } 
    } 
} 

在上面的代碼,s.Name是所有的源極我想要搜索的字符串。它只能正確輸入字符串的第一個字母。例如。其中s.Name可能是ABCDEF我想它繳費,當我鍵入它的任何子串,也許EFBC但不僅ABABC。我應該怎麼做?謝謝!

+0

[包含](https://msdn.microsoft.com/en-us/library/dy85x1sa(V = vs.110)的.aspx)似乎是你要找的,你可以使用的方法'布爾包含= s.Name.Contains(SUBNAME,StringComparison.OrdinalIgnoreCase);' –

+0

從你所描述的,你應該只設置[''TextBox.AutoCompleteCustomSource(HTTPS ://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletecustomsource(v = vs.110)的.aspx)至's.Name'並設置['TextBox.AutoCompleteMode']( https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletemode(v=vs.110).aspx)爲'AutoCompleteMode.Suggest' – Bolu

+0

感謝您的答覆。我重新編輯了我的問題。 – BarryLib

回答

1

我不會幫你想放棄所有的代碼,這樣就可以複製或粘貼。所以我打算給個想法.. 查看AutoCompleteSource,AutoCompleteCustomSource和AutoCompleteMode屬性。

textBox1.AutoCompleteMode = AutoCompleteMode.Suggest; 
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 >= 1) 
      { 
       //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; 
      } 
     } 
    } 

希望它有幫助。如果你還沒有理解,通知我。 欲瞭解更多信息,你可以喜歡這篇文章...... http://www.c-sharpcorner.com/article/autocomplete-textbox-in-C-Sharp/

+0

感謝您的回覆。但是SuggestString是什麼意思? – BarryLib

+0

[BurryLib](https://stackoverflow.com/users/7929687/barrylib)它實際上顯示一個或多個建議的完成字符串。有關更多信息,可以訪問https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletemode(v=vs.110).aspx。希望你明白了。如果還沒有得到。使用此鏈接..http://net-informations.com/q/faq/autocomplete.html。提醒我。如果你還沒有得到。 –

+0

再次感謝。但它似乎不起作用。我從[這裏](https://stackoverflow.com/questions/796195/c-sharp-autocomplete)讀取,現有的自動完成功能只支持前綴搜索,是嗎? – BarryLib