none
ComboBox SelectedValueChanged and SelectedIndexChanged event not firing when Text changed though SelectedItem changes

    Question

  • I have a bound Combobox with auto-complete, when i press delete to clear it - SelectedValueChanged event is not fired, but SelectedIndex become -1 andSelectedItem become NULL.
    Isn't it strange that SelectedIndex changes and event isn't fired??
    The only event firing is TextChanged event, and i can't handle it correctly because of auto-complete.

    So what i want is to handle situation when SelectedItem property changes.
    Sunday, December 13, 2009 7:51 AM

Answers

  • "I can understand the design purpose. If it fires the SelectedIndexChanged at that point, then each letter change will fire the event. That will cause other problem."

    Not necessary each letter change, it should fire the event only when SelectedIndex changes to a new value.
    In this case: text change->no item match found-> fire -1->type next letter-> no item found-> we still have "-1"->don't fire event..

    Quote from MSDN about ComboBox. SelectedIndexChanged Event : "Occurs when the SelectedIndex property has changed."
    it's not a bug -- it's a feature

    It's pointless to continue this conversation, i have written my own ComboBox control which works as i want, so the problem is solved.
    • Marked as answer by Alex Burtsev Wednesday, December 16, 2009 6:09 PM
    Wednesday, December 16, 2009 5:38 PM

All replies

  • Hi,

    The following code seems to work fine.

    Could you try it

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                DoSomething(comboBox1.SelectedIndex);
            }
    
            private void comboBox1_TextChanged(object sender, EventArgs e)
            {
                DoSomething(comboBox1.SelectedIndex);
            }
            private void DoSomething(int selectedIndex)
            {
                this.Text = selectedIndex.ToString();
            }
    Sunday, December 13, 2009 8:38 AM
  • I can't use TextChanged because my DoSomething () contains lots of slow code, and besides sometimes i need to change text in this event which will result in infinite loop, and i don't want to mess with disabling event handlers, my Form is pretty complicated and contans lots of combo-boxes tied together.

    I was just wondering is there a normal solution for this situation, i guess i will have to write a workaround like
    private
     void
     comboBox1_TextChanged(object
     sender, EventArgs e)
    {
    if (last_index!=combobox1.SelectedIndex)
    comboBox1.SelectedIndex = comboBox1.SelectedIndex //to fire event
    }

    Sunday, December 13, 2009 9:08 AM
  • Hi,

    Thank you for your feedback, I can reproduce the issue. Based on my test, when you call ComboBox.Items.Clear, the SelectedIndexChanged event doesn’t fire. When you remove the item which is currently selected, the event also doesn’t fire. I think it is the product issue.

    Do you mean the problem affects you on clear items situation happen? If so, can you store the selectedIndex in a variable then compare it with current selectedindex after clear method is called?

    Sincerely,
    Kira Qian
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Tuesday, December 15, 2009 8:33 AM
  • Based on my test, when you call ComboBox.Items.Clear
    "When i press delete to clear it?" - I mean when i clear the TextBox part of combobox not Items.Clear. Just select all text and delete it.


    In general ComboxBox works pretty strange:
    some short term i use:
    textpart - TextBox part of Combobox
    SelectedIndex =-1 assumes SelectedValue=null

    1) When you have SelectedIndex!=-1 and begin to type in textpart, SelectedIndex become -1 , SelectedIndexChanged event is not fired.
    2) When you typed some valid text (text that match one of Items in Combobox) and change focus SelectedIndex remains -1
    after this if you just open-close dropdown part of combobox without changing anything and change focus SelectedIndex become >=0 and SelectedItem contains this value, but SelectedIndexChanged event isn't fired again.
    3) Plus some bugs with Null value handling about which i wrote in other topic
    http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/2ee453ff-943a-4409-a9c6-e6d3ff3734d4


    This is all not real bugs, but i would rather call it unexpected behavior (because when SelectedIndex property change you expect SelectedIndexChanged event to fire).
    It is very obvious that Combobox consist of Text and List part and they don't work well together.
    Tuesday, December 15, 2009 9:46 AM
  • Hello,

    I have look into the source code through Reflector. When you change some text on the ComboBox, it does check the item’s text, if there is no item whose text matches the one in the TextBox, it will set the SelectedIndex to -1. It calls ListControl.SelectedIndex so the SelectedIndexChanged event doesn’t fired.

    I can understand the design purpose. If it fires the SelectedIndexChanged at that point, then each letter change will fire the event. That will cause other problem.

    Sincerely,
    Kira Qian
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Wednesday, December 16, 2009 7:47 AM
  • "I can understand the design purpose. If it fires the SelectedIndexChanged at that point, then each letter change will fire the event. That will cause other problem."

    Not necessary each letter change, it should fire the event only when SelectedIndex changes to a new value.
    In this case: text change->no item match found-> fire -1->type next letter-> no item found-> we still have "-1"->don't fire event..

    Quote from MSDN about ComboBox. SelectedIndexChanged Event : "Occurs when the SelectedIndex property has changed."
    it's not a bug -- it's a feature

    It's pointless to continue this conversation, i have written my own ComboBox control which works as i want, so the problem is solved.
    • Marked as answer by Alex Burtsev Wednesday, December 16, 2009 6:09 PM
    Wednesday, December 16, 2009 5:38 PM
  • Hello,

    I have look into the source code through Reflector. When you change some text on the ComboBox, it does check the item’s text, if there is no item whose text matches the one in the TextBox, it will set the SelectedIndex to -1. It calls ListControl.SelectedIndex so the SelectedIndexChanged event doesn’t fired.

    I can understand the design purpose. If it fires the SelectedIndexChanged at that point, then each letter change will fire the event. That will cause other problem. 
     

    Hi!

    I have this problem. Will be solve on this bug?


    • Edited by Al Le Friday, November 02, 2012 3:28 AM
    Friday, November 02, 2012 3:24 AM