按一个字母会激发combobox的selectedindexchanged和selectedvaluechanged事件吗?

时间:2013-06-20 11:04:32

标签: c# combobox selectedindexchanged

我的组合框中有很多值,它们按字母顺序排序。当我按一个字母来选择一个我想选择的值时,我会选择一个值,并且选择了值引发的事件,但我不希望这样。其实我还没有选择价值。我想要达到我想要选择的价值。为什么是这样?我们怎样才能阻止这种情况?

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

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分: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;

答案 2 :(得分:0)

如果您选择了某些内容,

选择了索引更改 如果当前文本不是其列表中的项目,则转为-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}
  }