实现效果:
知识运用:
ComboBox控件的FindString
public int FindString(string s) //查找数据项集合中指定数据项的索引
和Select方法
public void Select(int start,int length) //选择ComboBox可编辑部分中的文本范围
实现代码:
private bool state = false; //定义全局变量标识 private void cbox_Url_TextChanged(object sender, EventArgs e) { if (state) { string importText = cbox_Url.Text; //获得输入的文本 int index = cbox_Url.FindString(importText); //在ComboBox集合中查找匹配的文本 if (index >= 0) //有查找到结果时 { state = false; //关闭编辑状态 cbox_Url.SelectedIndex = index; //找到对应项 state = true; //打开编辑状态 cbox_Url.Select(importText.Length, cbox_Url.Text.Length); //设置文本选择长度 } } } private void cbox_Url_KeyDown(object sender, KeyEventArgs e) { state = (e.KeyCode != Keys.Back && e.KeyCode != Keys.Delete); cbox_Url.DroppedDown = true; //弹出下拉列表 }