2012-06-15 103 views
1

我有一个ComboBox这是数据绑定到stringsObservableCollectionComboBox也是可编辑的,因此您可以输入自己的值或从列表中选择一个值。我遇到的问题是SelectedItem的索引似乎是您在ComboBox中输入自己的值时选择的最后一项的索引,尽管当IsTextSearchEnabled设置为true时它为-1。如何在选择可编辑组合框时触发事件?

问题是,如果某人输入了自己的值,然后决定改为选择之前选择的ComboBox上的项目,则索引不会更改,因此SelectionChange事件不会触发。我怎么能在这种情况下发生事件?

回答

1

测试此... 我希望这有助于:

Dim oldSEL As String = "" 

'always checking while you move your mouse over the combobox (when altering selection) and using the keyboard to (alter selection) 
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.MouseMove, ComboBox1.KeyPress 
    Dim currentSEL As String = ComboBox1.SelectedText 
    If Not (oldSEL = "" And currentSEL = oldSEL) Then 
     fire() 
     oldSEL = currentSEL 
    End If 
End Sub 

Private Sub fire() 
    Trace.Write("text selected changed") 
End Sub 

你应该改变所有的Combobox1根据自己的喜好。

+0

这是一个很好的建议。我不是因为几个原因而使用它,而是让我想到如何做到这一点。我正在使用MouseLeave事件。但是,谢谢! – cost

+0

你可以使用这个想法来使用自定义事件和接口来创建更好的代码。但现在由你决定,因为我对这些并不熟悉。哈哈。 – jestrange