0

I have a textbox used to type in strings and display all the avaiable results in this textbox.The current code like follows:

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

In the above code, s.Name is the source of all the strings I want to search. It shall work only I correctly typed the first letter of the string. For example. One of the s.Name may be ABCDEF I want it avaiable when I type any sub string of it, maybe EF or BC but not only AB or ABC. How should I do this? Thanks!

BarryLib
  • 299
  • 1
  • 5
  • 20
  • [Contains](https://msdn.microsoft.com/en-us/library/dy85x1sa(v=vs.110).aspx) seems to be the method you're looking for, you could use `bool contains = s.Name.Contains(subName, StringComparison.OrdinalIgnoreCase);` – Keyur PATEL Aug 28 '17 at 06:48
  • From what you described, you should just set [`TextBox.AutoCompleteCustomSource `](https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletecustomsource(v=vs.110).aspx) to `s.Name` and set [`TextBox.AutoCompleteMode`](https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletemode(v=vs.110).aspx) to `AutoCompleteMode.Suggest` – Bolu Aug 28 '17 at 08:38
  • Thanks for reply. I have reedited my question. – BarryLib Aug 28 '17 at 08:45
  • check this: https://stackoverflow.com/questions/1437002/winforms-c-sharp-autocomplete-in-the-middle-of-a-textbox – Bolu Aug 28 '17 at 08:59
  • NO. The most voted answer didn't achieve what I described. – BarryLib Aug 28 '17 at 09:45

1 Answers1

0

I am not going to help you like giving all code, so that you can copy or paste. So I am going to give an idea.. Check out the AutoCompleteSource, AutoCompleteCustomSource and AutoCompleteMode properties.

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

Hope it helped. Notify me if you have not understood yet. For more information, you can like this article... http://www.c-sharpcorner.com/article/autocomplete-textbox-in-C-Sharp/

Vikas Gupta
  • 1,183
  • 1
  • 10
  • 25
  • Thanks for your reply. But what does SuggestString mean? – BarryLib Aug 28 '17 at 07:11
  • [BurryLib](https://stackoverflow.com/users/7929687/barrylib) It actually display one or more suggested completion strings. For more information, you can go to https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletemode(v=vs.110).aspx. Hope you got that. If still not getting. Use this link..http://net-informations.com/q/faq/autocomplete.html. Notify me. If you still not getting. – Vikas Gupta Aug 28 '17 at 07:14
  • Thanks again. But it seems don't work. I read from [Here](https://stackoverflow.com/questions/796195/c-sharp-autocomplete) that the existing AutoComplete functionality only supports searching by prefix, is ti right? – BarryLib Aug 28 '17 at 07:59
  • I think, that totally depends on where you want to start the search..Like in above example, I have provided some length i.e. t.Text.Length >= 1. And you also wanted like that..And yes it is searching be prefix method. I thought that is all you wanted.[BarryLib](https://stackoverflow.com/users/7929687/barrylib) – Vikas Gupta Aug 28 '17 at 08:09
  • what I want is when I type the substring of the targetstring e.g. `BC` of `ABCDEF` the textbox can also display the avaiable source.But now the above code only applys to `AB`, `ABC`,`ABCD` etc. – BarryLib Aug 28 '17 at 08:36