0

我的组合框中有很多值,它们按字母顺序排列。当我按下一个字母来选择一个我想要的值时,selectedindexchanged 和 selectedvaluechanged 事件被触发,但我不想要这个。实际上我还没有选择值。我正在尝试达到我想要选择的值。为什么是这样?我们怎样才能防止这种情况发生?

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

3 回答 3

1

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

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

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

于 2013-06-20T11:11:06.487 回答
0

如果您不想创建新的用户控件或在事件处理程序中处理它,您可以使用组合框的 AutoCompleteSource 属性。当用户使用回车按钮完成选择或控件失去焦点或用户使用鼠标单击选择项目时,这将引发 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;
于 2013-06-20T11:39:36.533 回答
0

如果您选择某些内容,则 selected 索引会更改,如果当前文本不是其列表中的项目,则变为 -1。这可能是更改文本的结果

值更改基本上会触发,因为无论文本是否在列表中都已更改。

我不太清楚你想做什么;但也许你想结合行为,所以只能通过你的代码;像这样的伪代码:

 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}
  }
于 2017-06-14T06:48:35.213 回答