2012-06-15 142 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