2014-10-30 89 views
3

我一直在使用代码AutoCompleteStringCollection如何在C#中使用AutoComplete获取事件“item selected”?

private void txtS_TextChanged(object sender, EventArgs e) 
    { 
     TextBox t = sender as TextBox; 
     string[] arr = this.dbService.GetAll(); 

     if (t != null) 
     { 
      if (t.Text.Length >= 3) 
      { 
       AutoCompleteStringCollection collection = new AutoCompleteStringCollection(); 
       collection.AddRange(arr);      
       this.txtSerial.AutoCompleteCustomSource = collection; 
      } 
     } 
    } 

我怎样才能为“选择项”用户选择自动完成建议后的事件?和领域的价值?

+0

从自动完成列表中选择的项目? – 2014-10-30 00:27:19

+0

是的。从“自动完成”列表中选择的项目。 – 2014-10-30 00:31:21

回答

5

没有这样的事情,我相信你用于自动完成的文本框的选择项目事件。你可以做的是添加一个按键事件到你的文本框。您可以验证是否按下了回车键(单击建议的链接与按回车键相同)。类似的东西:

private void textBox1_KeyDown(object sender, KeyEventArgs e) { 
    if (e.KeyData == Keys.Enter) { 
     String selItem = this.textBox1.Text; 
    } 
} 
+0

太棒了!谢谢! – 2014-11-05 03:41:35

1

简短的回答:做一个自定义事件

龙答: 您可以拦截你的文本框的KeyDown事件的数字键盘输入或正常的输入和工具箱的鼠标双击事件比较工具箱的内容,然后触发一个事件,如果他们匹配,代表会拿起。

0

与其专注于检测是否选择自动完成列表中的项目,而应该检查文本框的当前值是否在自动填充项目集合中。

if (txtSerial.AutoCompleteCustomSource.Contains(t.Text)) 
{ 
    // Logic to handle an exact match being selected 
    ... 
} 
else 
{ 
    // Update the autocomplete entries based on what was typed in 
} 

如果这恰好是是自动完成的值列表内的确切字符串类型的用户 - 或 - 他们选择从自动完成列表中值 - 这应该产生任何不同的行为?我认为在大多数情况下它不应该。