Winform ComBox模糊查询

时间:2021-08-23 14:44:20

一、添加数据源并绑定

List<string> list = new List<string>();
list.Add("张三");
list.Add("李四");
list.Add("王五");
list.Add("老王"); comboBox1.Items.AddRange(list.ToArray());

二、添加TextUpdate事件

 private void comboBox1_TextUpdate(object sender, EventArgs e)
{
List<string> listNew = new List<string>();
comboBox1.Items.Clear();
string str = comboBox1.Text;
foreach (var item in list)
{
if (item.Contains(str))
{
listNew.Add(item);
}
}
comboBox1.Items.AddRange(listNew.ToArray());
comboBox1.SelectionStart = comboBox1.Text.Length;
Cursor = Cursors.Default;
this.comboBox1.DroppedDown = true;//自动展开下拉框
}

三、效果

Winform ComBox模糊查询