int c_count = 0; //储存文本长度 private void comb_goods_name_KeyUp(object sender, KeyEventArgs e) { if (comb_goods_name.Text.Length != c_count && comb_goods_name.Text.Length != 0) //如果事件触发时文本长度和原长度不一致同时不为空时触发 { listgm.Clear(); string text = comb_goods_name.Text; //把文本框值赋给text listgm = gb.FindLike(comb_goods_name.Text); //根据用户输入的值查询 DataTable ta = new DataTable(); //绑定数据到 comb_goods_name ta.Columns.Add("ID"); ta.Columns.Add("名称"); foreach (GoodsMOD item in listgm) { DataRow row = ta.NewRow(); row["ID"] = item.Id; row["名称"] = item.Goods_name; ta.Rows.Add(row); } comb_goods_name.DataSource = ta; comb_goods_name.DisplayMember = "名称"; comb_goods_name.ValueMember = "ID"; Cursor = Cursors.Default; // 重新获取鼠标 this.comb_goods_name.DroppedDown = true; //弹出下拉列表 comb_goods_name.Text = text; //把text的值赋给文本框 comb_goods_name.SelectionStart = comb_goods_name.Text.Length; //把光标移到最后面 } c_count = comb_goods_name.Text.Length; //把当前文本长度赋给c_count }