按下字母将触发组合框'selecdindexchanged和selectedvaluechanged事件

本文关键字:selecdindexchanged 事件 selectedvaluechanged 组合 | 更新日期: 2023-09-27 18:19:06

我的组合框中有很多值,它们是按字母顺序排列的。当我按下一个字母来选择一个值,我想要selecteddexchanged和selectedvaluechanged事件被触发,但我不希望这样。实际上我还没有选择值。我试图达到我想要选择的值。为什么会这样?我们如何预防这种情况?

AUDI
BMW
CITROEN
D...
E...
MERCEDES -> To select this. pressing M fires events???
OPEL
VOLVO

按下字母将触发组合框'selecdindexchanged和selectedvaluechanged事件

在你的SelectionChanged事件处理程序中,你可以通过在ComboBox弹出窗口打开时设置e.Handled = true来处理这个问题。如果弹出窗口关闭,您可以继续在SelectionChanged事件中执行实际步骤。

或者您可以创建自己的自定义控件,继承自ComboBox。在该类的构造函数中,为SelectionChanged添加一个事件处理程序,并在弹出窗口打开时通过设置e.Handled = true将事件标记为已处理。你可能不得不在你的自定义控件中处理订阅/标记哪些事件,以使事情按照你想要的方式运行。

注意:你可以通过处理DropDown和DropDownClosed事件来检查组合框弹出是否打开。: -)

如果你不想创建新的用户控件或者在事件处理程序中处理它,你可以使用组合框的AutoCompleteSource属性。当用户使用enter按钮完成选择或控件失去焦点或用户使用鼠标单击选择项目时,这将引发SelectedIndexChanged。为此,将以下属性设置为组合框。

this.comboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.comboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
List<string> testString = new List<string>{"abcd", "acde", "adef", "bcde", "bdef", "befg", "cdef", "defg", "cfgh"};
this.comboBox.DataSource = testString;
this.comboBox.SelectedIndexChanged += this.ComboBoxOnSelectedIndexChanged;

所选索引随您选择的内容而变化如果当前文本不是其列表中的项,则变为-1。这可能是改变文本

的结果

value change基本上会触发,因为无论文本是否在列表中,它都会发生变化。

我不太清楚你想做什么;但也许你想把这些行为结合起来,所以只落在你的代码里;如下所示的伪代码:

 inside your value changedevent()
  {
   // first you might put in some text reformating code here
   // so for example you might replace spaces for '-' or so..
   // or if one presses M,(and text length =1
   // the list of elements in the control 
   // could be filtered on items starting with an M
   // by using a Linq query or so on a list or array.

   If (yourcombobox.selectedindex > -1) {// fire my code as now we're sure its an existing element}
  }
相关文章:
  • 没有找到相关文章