如何建议附加包含字符串的组合框 [英] How to SuggestAppend a ComboBox containing a string

查看:83
本文介绍了如何建议附加包含字符串的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to have my ComboBox items suggest and append its items when something is contained in them, not just via the StartsWith function.

我的ComboBox绑定到一个DataView,该视图包含一个长连接的客户端[ CompanyName ],[地址],[城市].

My ComboBox is bound to a DataView which contains clients [CompanyName], [Address], [City] in a long concatenation.

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

I want my users to be able to type in the city and still find the records which matches with all of the fields above. I know this is possible with Infragistics but I don't have that package.

搜索词:""

  • Costco,第一大街123号,谢尔布鲁克
  • Provigo,344 Ball Street,谢尔布鲁克
  • Sher 盒子,蒙特利尔第七街93号
  • Costco, 123 1st Avenue, Sherbrooke
  • Provigo, 344 Ball Street, Sherbrooke
  • Sherbox, 93 7th Street, Montreal

在VB.Net中这是否可行,还是我应该搜索其他内容?

Is this possible in VB.Net or should I be searching for something else?

推荐答案

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

I did some research and found the following question:

替代Winforms ComboBox自动填充建议规则

在这个问题上,他们提到另一个问题:

In that question they reffer to another question:

C#自动填充

让我们引用该问题的最佳答案

现有的自动完成功能仅支持按 字首.似乎没有任何体面的方法可以覆盖 行为.

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

有人通过以下方式实现了自己的自动填充功能 覆盖OnTextChanged事件.那可能是您最好的选择.

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

例如,您可以在TextBox的正下方添加ListBox并设置 其默认可见性为false.然后您可以使用OnTextChanged TextBox事件和SelectedIndexChanged事件 ListBox显示和选择项目.

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.

这篇关于如何建议附加包含字符串的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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