我正在试着想出一个解决方案,让我可以在包含特定单词/短语的组合框中搜索项目。我尝试使用AutoComplete文本框功能,但它只搜索对我没有好处的第一个单词。
我已经发起了两个列表
public List<string> listOnit = new List<string>();
public List<string> listNew = new List<string>();然后,我将数据加载到comboBox中
if (rdr.HasRows == true)
{
// var source = new List<string>();
while (rdr.Read())
{
// myCollectionSales.Add(rdr[0].ToString());
listOnit.Add(rdr[0].ToString());
}
rdr.Close();
//textBox1.AutoCompleteCustomSource = myCollectionSales;
comboBox1.Items.AddRange(listOnit.ToArray());
}并使用TextUpdate事件处理程序在文本发生更改时过滤列表
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
comboBox1.Items.Clear();
listNew.Clear();
foreach (var item in listOnit)
{
if (item.Contains(this.comboBox1.Text))
{
listNew.Add(item);
}
}
comboBox1.Items.AddRange(listNew.ToArray());
comboBox1.SelectionStart = this.comboBox1.Text.Length;
Cursor = Cursors.Default;
comboBox1.DroppedDown = true;
}我遇到了一个问题,搜索结果没有返回我期望的结果。例如,我搜索字符串"Bud“,得到的结果如下
http://prntscr.com/ppkatd
在数据库中,还有百威33cl和Keg Budweiser (http://prntscr.com/ppkbu4),例如,它被放在第一个列表中。
我应该使用不同的方法,而不是“包含”吗?
发布于 2019-10-29 18:09:01
也许你使用的是不同的案例?
尝试使用.ToLower():
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
comboBox1.Items.Clear();
listNew.Clear();
foreach (var item in listOnit)
{
if (item.ToLower().Contains(this.comboBox1.Text.ToLower()))
{
listNew.Add(item);
}
}
comboBox1.Items.AddRange(listNew.ToArray());
comboBox1.SelectionStart = this.comboBox1.Text.Length;
Cursor = Cursors.Default;
comboBox1.DroppedDown = true;
}https://stackoverflow.com/questions/58605186
复制相似问题