ComboBox SelectedIndexChanged event not fired on Enter

I am working on VS 2010 with VB using the .NET Framework 4.0

I have a combobox . It has some elements and it displays just fine. Here where it gets a little weird:

If I click the dropdown arrow on combobox and CLICK on the item I want, SelectedIndexChanged is called - fine.

If I click inside the combobox text area and start typing what I want and end it by pressing the up (or down) key called SelectedIndexChanged is fine too.

If I click the drop-down arrow on combobox and start typing what I want and end it by pressing ENTER, SelectedIndexChanged not called - PROBLEM.

Is there any other event triggered by ENTER in the latter case? I tried using the TextChanged and TextUpdate , but they don't seem to work:

 Private Sub cmbStatus_TextChanged(sender As System.Object, e As System.EventArgs) Handles cmbStatus.TextChanged If e.Equals(Keys.Enter) Then Call SomeMethod() End If 

Should I use something other than e.Equals(Keys.Enter) ?

Is there any other event that I should look for?

EDIT: Example elements in a combobox :

  • 10 - CHECKING NEW ENTRANCE AND COMPLETENESS ---> this is the most common type
  • 13 - TRB / HRB ASSIGNED ---> there are a few with '/'
  • 60 - EXTERNAL (KEEP ADDITIONAL NOTICE) ---> there are a few with '(' and ')'

In principle, the type of each list is "## - SOME TEXT".

+7
source share
9 answers
 Option Strict On Public Class Form1 Friend WithEvents ComboBox1 As New ComboBox With {.Parent = Me} Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load ComboBox1.Items.AddRange({"hello", "tes1ted", "word", "item", "tes2ted"}) ComboBox1.Text = ComboBox1.Items(0).ToString End Sub Private Sub ComboBox1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles ComboBox1.KeyUp 'You can put this in the keydown event, or adapt it a small bit and put it in the keypress event 'putting it in the textchanged event is problematic and not recommended. Dim OriginalText As String = ComboBox1.Text If e.KeyCode = Keys.Enter Then If ComboBox1.SelectionLength > 0 Then ComboBox1.Text = ComboBox1.Text ComboBox1.SelectionLength = 0 ComboBox1.SelectionStart = ComboBox1.Text.Length End If End If If Not IsTextKey(e.KeyCode) Then Exit Sub Dim Filter As String = ComboBox1.Text & "*" If Filter.Length = 1 Then Exit Sub For I = 0 To ComboBox1.Items.Count - 1 If LCase(ComboBox1.Items(I).ToString) Like LCase(Filter) Then ComboBox1.SelectedItem = ComboBox1.Items(I) ComboBox1.Select(OriginalText.Length, (ComboBox1.Text.Length - OriginalText.Length)) Exit Sub End If Next End Sub Function IsTextKey(ByVal Key As Integer) As Boolean Select Case True Case Key = Keys.Up : Return False Case Key = Keys.Down : Return False Case Key = Keys.Left : Return False Case Key = Keys.Right : Return False Case Key = Keys.Back : Return False Case Key = Keys.Delete : Return False Case Key = Keys.LWin : Return False Case Key = Keys.RWin : Return False 'add whatever I missed 'return false if the key either removes text from the textbox 'or does not produce a character Case Else 'return true if the key produces a visible character(including space) Return True End Select End Function End Class 
+4
source

Disclaimer: it is written in C # - let me know if you need to translate it to VB.

 private void comboBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { //It important to also check that the Combo Box is displaying its Drop Down. If //you want this to execute even when it is not displayed, remove the check for //comboBox1.DroppedDown. if (e.KeyCode == Keys.Enter && comboBox1.DroppedDown && !string.IsNullOrEmpty(comboBox1.Text)) { int index; //Attempt to locate the string typed in by the user. An index of -1 //means it was not found. if ((index = comboBox1.FindStringExact(comboBox1.Text)) != -1) { //Update the SelectedIndex. comboBox1.SelectedIndex = index; } } } 

Interestingly, the docs say that we should use the SelectionChangeCommitted event instead of the SelectedIndexChanged event when processing the selection changes made by the User. This needs to be done in this case, since the SelectedIndexChanged event fires twice using my approach.

Edit:

To prevent the user from entering the entire line, use the advice from Adi's answer: go to the properties of the combo box and set the AutoCompleteMode parameter to SuggestAppend and AutoCompleteSource to ListItems - I actully used these settings when creating my answer, so it should work for you.

+6
source
 Private Sub ComboBox1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp If e.KeyCode = Keys.Enter Then MessageBox.Show(ComboBox1.SelectedText) Call SomeMethod() End If End Sub 
+4
source

I believe that you should set AutoCompleteMode to SuggestAppend and AutoCompleteSource to ListItems . Thus, if what you enter is stable with the elements loaded into the ComboBox, writing it and finding this element, pressing Enter will launch SelectedIndexChanged (even if no match is found, the first one in the list will be selected)

I have prepared something to point this out to you.

Hi,

Adi konstantin

+3
source

Sign up for the KeyPressed event:

 Private Sub yourComboBox_KeyPressed(sender As System.Object, e As System.KeyPressedEventArgs) Handles yourComboBox.KeyPressed If e.KeyChar.Equals((char)Keys.Enter) Then Call SomeMethod() End If 
+2
source

it will help your problems.

  Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown If e.KeyCode = Keys.Enter Then MsgBox("hello")'call some functions End If End Sub 
+2
source

Can you tell me if this works for you?

  Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress If e.KeyChar = Chr(13) Then ComboBox1_SelectedIndexChanged(sender, e) End If End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged MsgBox(ComboBox1.Text) 'Your code here End Sub 
+2
source

I had a similar problem when the combobox dropdown was set to a simple one, with autocomplete set to add, the source from my lists. My workaround was to have a string variable storing combobox text for each sequence-changing event. Thus, when the ENTER key is pressed, you can get the deleted text and reassign it to the list text.

'Global Declaration

 'Global declaraion dim selected_text as string = "" Private Sub combobox_textchanged(sender As Object, e As EventArgs) Handles combobox.TextChanged If combobox.Text IsNot Nothing And combobox.Text <> "" Then selected_text = combobox.Text End If End Sub Private Sub combobox_keydown(sender As Object, e As KeyEventArgs) Handles combobox.KeyDown If e.KeyCode = Keys.Enter Then combobox.Text = selected_text 'put logic here End If End Sub 

There are some excellent answers above, but I liked that I didn’t have to disassemble all the command keys and arrows, etc.

+2
source

Close the list manually at the PreviewKeyDown event:

 Private Sub cmbStatus_PreviewKeyDown(sender As ComboBox, e As PreviewKeyDownEventArgs) Handles cmbStatus.PreviewKeyDown If e.KeyCode = Keys.Enter Then sender.DroppedDown = False End Sub 
0
source

All Articles