如何 SuggestAppend 包含字符串的 ComboBox [英] How to SuggestAppend a ComboBox containing a string

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

问题描述

我想让我的 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.

My ComboBox 绑定到一个 DataView,其中包含客户 [CompanyName]、[Address]、[City] 的长串联.

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.

搜索词:Sher"

  • Costco, 123 1st Avenue, Sherbrooke
  • Provigo, 344 Ball Street, Sherbrooke
  • Sher盒子,蒙特利尔第 7 街 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.然后你可以使用 OnTextChangedTextBox 事件和 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.

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

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