Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ComboBox Event Problems 1

Status
Not open for further replies.

keyser456

IS-IT--Management
Nov 21, 2003
73
US
Maybe I'm not understanding ComboBox events properly but it seems like these events aren't working as they should be.

I have a System.Windows.Forms.ComboBox placed on a form and its Items list is populated with items. When I open the dropdown box and click on an item, the SelectedIndexChanged and SelectedValueChanged events fire as expected.

However, after typing in a text value that is found in the dropdown box, when you hit enter the dropdown box closes and it seems to me like these events should fire, but they don't. When you reopen the dropdown box the item is indeed now selected, but the SelectedValueChanged and the SelectedIndexChanged never fired.

I trapped the SelectedIndexChanged, SelectedValueChanged, and SelectionChangeCommitted events of the ComboBox and did some testing. I also added a button to print the status of the ComboBox's SelectedIndex, SelectedValue, and SelectedItem properties when I click it. (I have step by step descriptions of what I did).

This appears when the form is loading:
{
SelectedValueChanged
SelectedIndexChanged
SelectedValueChanged
SelectedIndexChanged
}

After the form loaded, I clicked the test button to check the ComboBox's current property values:
{
SelectedIndex: -1
SelectedValue: null
SelectedItem: null
}

Next, I typed in the first number that displays in my dropdown box (in my case 1111) and hit enter. The text stays in the textbox portion and the dropdown box disappears. I then clicked the test button to check the current values. As you can see none of the values have changed.
{
SelectedIndex: -1
SelectedValue: null
SelectedItem: null
}

So you'd expect no item to be selected when you reopen the dropdown box, right? Wrong. I click on the little arrow of the ComboBox to open the dropdown box and there is the first item (1111) selected!!! I immediately click the button to display the ComboBox property values and sure enough, the SelectedIndex and SelectedItem have changed, but the SelectedValue is still null. However, the SelectedIndexChanged event NEVER fired. None of my trapped events fired, yet the index has obviously been changed.
{
SelectedIndex: 0
SelectedValue: null
SelectedItem: 1111
}

I then click on the dropdown box and type in the next number (1860) and hit enter. Again the dropdown box disappears. I click the button to test the values. Nothing has changed yet:
{
SelectedIndex: 0
SelectedValue: null
SelectedItem: 1111
}

I click the dropdown box though, and there's 1860 selected. Again I immediately click my button to test the values:
{
SelectedIndex: 1
SelectedValue: null
SelectedItem: 1860
}

Mind you none of my trapped events have fired during any of these SelectedIndex and SelectedItem changes. Just for the heck of it I open my dropdown box and actually click the next item (1862). Sure enough the events fire:
{
SelectionChangeCommitted
SelectedValueChanged
SelectedIndexChanged
}

This is what I would expect and shows that my events are hooked correctly, but it seems as though this should happen when you hit enter as well, but it doesn't. I clicked my button to test the ComboBox's property values:
{
SelectedIndex: 2
SelectedValue: null
SelectedItem: 1862
}

Can anyone explain this?

All I'm after is I when you hit enter I want it to pick up the fact there is a SelectedIndex change. The problem is the change doesn't actually occur unless you click on the dropdown box again, and even then the event never actually fires although the SelectedIndex does change. I'm so confused.
 
It sounds like you have the DropDownStyle of the control set to DropDown. This means that the user can type whatever they like, not just things which appear in the list. If you've typed '1111' then you could still continue typing '1111 abc Coconut' if you wanted to. This value - or any other value the user enters manually - wouldn't have an index, therefore the SelectedIndexChanged won't be fired.

If you change the DropDownStyle property to DropDownList the user will only be able to enter values that appear in the list. Whenever the value they have typed matches an item other than the currently-selected one, the index will change and trigger the event. If your list contains 'abc' and 'abxy', the first item will be selected when they press 'a', it will remain selected when they press 'b' and the second item will become selected when they press 'x'.

If you don't want the user to be restricted to the list but you do want the event to be fired whenever what they've typed matches something in the list you'll need to write some code for the TextChanged event (if you want it to occur as they type) or the Leave event (if you want it to occur when they leave the control).

Hope that helps!

Nelviticus
 
I really don't like the behavior when using the DropDownList style and I was trying to avoid adding more code (to search the ListItems after text is entered for a match when the text entered is a direct match, it doesn't seem unreasonable for a combobox to accomplish this), but it looks like it is inevitable. IMO there should be absolutely no way a ComboBox's SelectedIndex property value can change without the SelectedIndexChanged event firing, but I guess Microsoft disagrees so what can I do? Adding workarounds rather than directly addressing problems seems to be a recurring theme with them so maybe they expect us to follow suit? Anyway, thanks for the info.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top