Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ComboBox's SelectedIndexChanged event not being called on Enter

Tags:

vb.net

I'm working on VS 2010 with VB using .NET Framework 4.0

I have a combobox. It has some items in it and displays just fine. Here's where it gets a little weird:

If I click the drop-down arrow on the combobox and CLICK on the item I want, SelectedIndexChanged is called - good.

If I click inside the text area of the combobox and start typing what I want selected and finish it by pressing the up (or down) key, SelectedIndexChanged is called - also good.

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

Is there a different event that is caused by the ENTER in the last case? I've tried using the TextChanged and TextUpdate events, but those do not seem to be working:

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 besides e.Equals(Keys.Enter)?

Is there another event I should be looking for?

EDIT: An example of the items in the ComboBox are:

  • 10 - NEW ENTRY AND COMPLETENESS CHECK ---> this is the most common type
  • 13 - ASSIGNED TO TRB/HRB ---> there are a few with '/'
  • 60 - EXTERNAL (HOLD UNTIL FURTHER NOTICE) ---> there are a few with '(' and ')'

Basically, the type of each listing is "## - SOME TEXT".

like image 865
redhotspike Avatar asked Oct 05 '12 16:10

redhotspike


People also ask

How do you prevent SelectedIndexChanged event when datasource is bound?

You may use Tag property of combobox. It can be empty or 0 integer value when it's empty or have not fulfilled yet. You have to set combobox's Tag as count of it's items after bounding. In SelectedValueChanged event if the Tag property is null or 0 you have to return from void.

How do you stop a combobox SelectedIndexChanged event from firing when the form loads?

You can simply unbind the SelectedIndexChanged event, call your fill function and bind the SelectedIndexChanged event again.

Which event can be used to detect changes in list Combo box selection?

Remarks. The SelectionChangeCommitted event is raised only when the user changes the combo box selection, and you can create a handler for this event to provide special handling for the ComboBox when the user changes the selected item in the list.


1 Answers

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

private void comboBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    //It's 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 be using the SelectionChangeCommitted event instead of the SelectedIndexChanged event when handling selection changes made by the user. It is necessary to do so in this case as the SelectedIndexChanged event fires twice using my approach.

Edit:

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

like image 148
nick_w Avatar answered Oct 06 '22 10:10

nick_w