ComboBox SelectionChanged event fires on Text change

Answered ComboBox SelectionChanged event fires on Text change

  • Wednesday, May 20, 2009 9:43 AM
     
     

    I want to suppress the SelectionChanged event of ComboBox.

    My requirement is as follows -
    SelectionChanged event should be fired only when user selects any item from ComboBox.
    Currently it gets fired whenever ItemsSource changes or underlying binding value changes or Text changes.

    ComboBox1.ItemsSource = _dataList; //fires SelectionChanged.
    ComboBox1.Text = "something" //fires SelectionChagned.

    Is there anyway by which I can suppress or atlest detect the source of this event which caused it to fire?

    Thanks in advance.
    Varun

    • Moved by Harry Zhu Thursday, May 21, 2009 7:55 AM I'm moving the thread to winform forum , since it's relating to combox (From:Visual C# General)
    •  

All Replies

  • Wednesday, May 20, 2009 9:54 AM
     
     
    Hi,

    Whenever you are changing the data source of the combo box then the data items will get changed.
    The textchanged event will get fired when you are typing some thing in the combobox. It leads a selectionchanged Event because the data in the combo box is not the one which is there earlier.

    Regards,
    James
  • Thursday, May 21, 2009 5:50 AM
     
     

    Thanks for your reply.
    But what you are saying I already mentioned in my previous post.
    I have a specific requirement - I have to take certain action only when user selects some item from the combo box.
    So if I can know that what event (Either Text change or ItemsSource changed) has caused the SelectionChanged to get fired then I can accordingly perform some action on that.

  • Thursday, May 21, 2009 1:28 PM
    Moderator
     
     Answered
    Set a flag that indicates that your code is doing something that would cause the event to fire.  Check that flag in the event handler.  If it isn't set, you know it was the user changing the selection.

        private bool mChangingStuff;

        private void DoSomethingWithDataSource() {
          mChangingStuff = true;
          try {
            // etc..
          }
          finally {
            mChangingStuff = false;
          }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
          if (!mChangingStuff) {
            // User changed selection!
            //...
          }
        }


    Hans Passant.
    • Marked As Answer by Bruce.Zhou Friday, May 29, 2009 10:21 AM
    •  
  • Thursday, May 21, 2009 6:44 PM
     
     Proposed
    You could look at the SelectionChangeCommitted event and see if that is closer to what you need.
    www.insteptech.com
    • Proposed As Answer by DeborahKMVP Tuesday, May 26, 2009 5:07 PM
    •