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:
---> this is the most common type
---> there are a few with '/'
---> there are a few with '(' and ')'
Basically, the type of each listing is "## - SOME TEXT".
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.
You can simply unbind the SelectedIndexChanged event, call your fill function and bind the SelectedIndexChanged event again.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With