简体   繁体   中英

C# Create Custom Autocomplete TextBox

I want to create a TextBox where a autocomplete dropdown shows up with some suggestions.
Of course, I thought of using AutoCompleteCustomSource on my textbox, but the problem is, that the textbox automatically filters everything that does not contain the entered text.

For example, if I type "listen", my algorithm figures "listen (now)", "listen (later)" and "listen to AAA" as suggestions. When I put them in the autocompletecustomsource everything works fine. But as soon as I write "now" so that the text becomes "listen now", the automplete dropdown is empty, because none of the items in the autocompletecustomsource begins with "listen now".

What I tried next, was to change the input from a textbox to an combobox, where I put my suggestions in the Items property, and then just open the dropdown programmatically. The problem here is, that the first of the items gets automatically selected when I open the dropdown from code, and the text of the first item replaces the entered text.

Imagine the first example: when you type "listen", the dropdown opens with the items "listen (now)", "listen (later)" and "listen to AAA". But the text in the combobox changes automatically to the first item, thus becomes "listen (now)", and you cant type anything else.

this is the code I'm using at the moment:

    private void comboBox2_KeyUp(object sender, KeyEventArgs e)
    {
        string asd = comboBox2.Text;
        if (asd.Length < 3)
            return;

        if (e.KeyCode == Keys.Enter)
        {
            OpenItem(asd);
            return;
        }
        if (AllToString(comboBox2.Items).Contains(asd))
        {
            return;
        }

        DateTime started = DateTime.Now;
        System.Threading.Thread tth = new System.Threading.Thread((System.Threading.ThreadStart)delegate()
            {
                JsonData dat = new JsonData();
                //Query autocomplete
                ...
                //End Query
                comboBox2.Invoke((MethodInvoker)delegate()
                {
                    if (comboBox2.Tag == null || ((DateTime)comboBox2.Tag) < started)
                    {
                        comboBox2.Items.Clear();
                        comboBox2.Items.AddRange(li.ToArray()); //li is the list of suggestions
                        comboBox2.Select(comboBox2.Text.Length, 0);
                        comboBox2.Tag = started;
                        if (li.Count != 0)
                            comboBox2.DroppedDown = true;
                        else
                        {
                            comboBox2.Focus();
                            comboBox2.Select(comboBox2.Text.Length, 0);
                        }
                    }
                });
            });
        tth.IsBackground = false; tth.Start();
    }

So my question is: how can I create a text or combobox where I can put my suggestions in a drop-down, without changing the entered text and without filtering. I want all suggestions to get displayed all the time.

Thanks for your help, Alex

The better is to create a new class which herite of the combo box and override events

   public class myCombo : ComboBox
    {
        protected override void OnPaint(PaintEventArgs e)
        {


            base.OnPaint(e);
        }
    }

I do something to change the display .. to put a grid but it's a long time ago.

try to search on this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM