2017-08-28 128 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