1.设置Combox属性: DropDownStyle:DropDown
2.添加TextUpdate事件
3.下列为Name = cb_material 的 combox 控件
private void cb_material_TextUpdate(object sender, EventArgs e)
{
string s = this.cb_material.Text; //获取cb_material控件输入内容
List strList = new List(); //存放原始数据(可以是对象,字符串...)
strList.AddRange(materials.ToArray()); // List materials
List strListNew = new List();
//清空combobox
this.cb_material.Items.Clear();
//遍历全部备查数据
foreach (var item in strList)
{
// 根据输入的值模糊查询,将符合条件的值存储到新strListNew的集合里面
if (item.shape.Contains(this.cb_material.Text))
{
strListNew.Add(item);
}
}
if (strListNew.Count >= 1) // 存在符合条件的内容
{
//将符合条件的内容加到combobox中
this.cb_material.Items.AddRange(strListNew.ToArray());
}