目标
我想让我的 ComboBox 项目建议并在其中包含某些内容时附加其项目,而不仅仅是通过 StartsWith 函数。
我的 ComboBox 绑定(bind)到包含客户端 [ 的 DataView公司名称 ], [ 地址 ], [ 城市 ] 在长连接中。
我希望我的用户能够输入城市并仍然找到与上述所有字段匹配的记录。我知道 Infragistics 可以做到这一点,但我没有那个包。
搜索词:“ 谢尔 ”
这在 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 theTextBox
and set its default visibility to false. Then you can use theOnTextChanged
event of theTextBox
and theSelectedIndexChanged
event of theListBox
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/