0

I have the following code which only works when looking up the first 3 characters at the start of the line. However, I want to be able to show AutoComplete data by searching anywhere in the line.

private void txtCMDestination_TextChanged(object sender, EventArgs e)
{
    try
    {
        TextBox t = sender as TextBox;
        if (t != null)
        {
            if (t.Text.Length >= 3)
            {
                var arr = IndexItems.Where(x => x.ToUpper().Contains(t.Text.ToUpper())).ToArray();
                var collection = new AutoCompleteStringCollection();
                collection.AddRange(arr);
                txtCMDestination.AutoCompleteCustomSource = collection;
            }
        }
    }
    catch (Exception) { }
}

Does anyone know what I am doing wrong here?

Cheers.

EDIT

I have debugged to ensure that var arr contains filtered data when I type in the 3 chars and it does. So my LINQ is working as expected, which leads to me to believe there is something wrong with txtCMDestination.AutoCompleteCustomSource = collection; receiving the collection?

Magic Mick
  • 1,475
  • 1
  • 21
  • 32
  • 1
    http://stackoverflow.com/questions/8779557/dynamically-changing-textboxs-autocomplete-list-causes-accessviolationexception – MSL Oct 04 '16 at 06:32
  • http://stackoverflow.com/questions/3018029/autocomplete-texbox-error-write-to-protected-memory – MSL Oct 04 '16 at 06:32
  • What is the question? Is your issue that the AC dropdown doesn't show? I'm afraid it insists on looking only at the start just like it insists on sorting by alphabet, no matter what you feed in.. So you may have to work around with a fully coded solution. – TaW Oct 04 '16 at 15:18
  • @TaW thanks for the reply. My question is exactly that, how can I get it to look in the middle of a string rather than the start. I am able to get the list filtered, it does not drop down though. Is there a way to drop down/show the list manually? – Magic Mick Oct 04 '16 at 22:07
  • As I recall, there is a `SendMessage` API `CB_SHOWDROPDOWN` with ComboBox, but is there something similar for using TextBox? – Magic Mick Oct 04 '16 at 22:13
  • What about something like this? http://net-informations.com/q/faq/autocomplete.html –  Dec 27 '16 at 20:12

0 Answers0