C#创建自定义自动完成文本框 [英] C# Create Custom Autocomplete TextBox

查看:134
本文介绍了C#创建自定义自动完成文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个文本框,其中会显示自动完成下拉列表并提供一些建议.
当然,我想到了在文本框中使用AutoCompleteCustomSource,但是问题是,文本框会自动过滤不包含输入文本的所有内容.

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.

例如,如果我键入"listen",则我的算法会建议使用"listen(现在)","listen(以后)"和"listen to AAA"作为建议.当我将它们放入autocompletecustomsource中时,一切正常.但是,一旦我写了"now"(现在)以使文本变为"listen now"(立即收听),则automplete下拉列表为空,因为autocompletecustomsource中的所有项都不以"listen now"(开头)开始.

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".

接下来我要尝试的是将输入从文本框更改为组合框,然后在其中将我的建议放在Items属性中,然后以编程方式打开下拉列表.这里的问题是,当我从代码中打开下拉菜单时,第一个项目会自动被选择,并且第一个项目的文本会替换输入的文本.

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.

想象一下第一个示例:当您键入"listen"时,下拉列表将打开,其中包含项"listen(现在)","listen(以后)"和"listen to AAA".但是,组合框中的文本会自动更改为第一项,因此变为听(现在)",并且您无法输入其他任何内容.

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.

尝试对此进行搜索.

这篇关于C#创建自定义自动完成文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆