vb.net - 如何建议附加一个包含字符串的组合框

标签 vb.net winforms combobox autocomplete contains

目标

我想让我的 ComboBox 项目建议并在其中包含某些内容时附加其项目,而不仅仅是通过 StartsWith 函数。

我的 ComboBox 绑定(bind)到包含客户端 [ 的 DataView公司名称 ], [ 地址 ], [ 城市 ] 在长连接中。

我希望我的用户能够输入城市并仍然找到与上述所有字段匹配的记录。我知道 Infragistics 可以做到这一点,但我没有那个包。

搜索词:“ 谢尔

  • 好市多,第一大道 123 号,谢尔 布鲁克
  • Provigo,鲍尔街 344 号,谢尔 布鲁克
  • 谢尔 蒙特利尔第 7 街 93 号盒子

  • 这在 VB.Net 中是否可行,还是我应该寻找其他东西?

    最佳答案

    我做了一些研究,发现了以下问题:

    Override Winforms ComboBox Autocomplete Suggest Rule

    在那个问题中,他们提到了另一个问题:

    C# AutoComplete

    Let's quote the best answer from that question

    The existing AutoComplete functionality only supports searching by prefix. There doesn't seem to be any decent way to override the behavior.

    Some people have implemented their own autocomplete functions by overriding the OnTextChanged event. That's probably your best bet.

    For example, you can add a ListBox just below the TextBox and set its default visibility to false. Then you can use the OnTextChanged event of the TextBox and the SelectedIndexChanged event of the ListBox to display and select items.

    This seems to work pretty well as a rudimentary example:

    public Form1()
    {
        InitializeComponent();
    
    
        acsc = new AutoCompleteStringCollection();
        textBox1.AutoCompleteCustomSource = acsc;
        textBox1.AutoCompleteMode = AutoCompleteMode.None;
        textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        acsc.Add("[001] some kind of item");
        acsc.Add("[002] some other item");
        acsc.Add("[003] an orange");
        acsc.Add("[004] i like pickles");
    }
    
    void textBox1_TextChanged(object sender, System.EventArgs e)
    {
        listBox1.Items.Clear();
        if (textBox1.Text.Length == 0)
        {
        hideResults();
        return;
        }
    
        foreach (String s in textBox1.AutoCompleteCustomSource)
        {
        if (s.Contains(textBox1.Text))
        {
            Console.WriteLine("Found text in: " + s);
            listBox1.Items.Add(s);
            listBox1.Visible = true;
        }
        }
    }
    
    void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
        hideResults();
    }
    
    void listBox1_LostFocus(object sender, System.EventArgs e)
    {
        hideResults();
    }
    
    void hideResults()
    {
        listBox1.Visible = false;
    }
    

    There's a lot more you could do without too much effort: append text to the text box, capture additional keyboard commands, and so forth.

    关于vb.net - 如何建议附加一个包含字符串的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26362806/

    相关文章:

    vb.net - 如何比较两个对象,就好像 Option Strict 关闭一样

    asp.net - 找出重点控制

    .net - Console.In.ReadLine(重定向的StdIn)卡住所有其他线程

    c# - 在 comboBox 中列出时将 DateTime String 转换为 Date-only

    qt - 如何限制QML中ComboBox下拉列表的大小

    vb.net - DataGridView 多行选择问题

    c++ - Windows 窗体 - 图片框。如何删除图像

    c# - 如何让命令的输出实时显示在窗体的控件中?

    c# - 使用套接字的Winforms C#应用程序可在winXp下工作,但在Windows 7下会引发错误

    python - 如何在 tkinter Python 3.7 中为组合框下拉菜单绑定(bind)按键事件