Search code examples
c#eventscomboboxselectedindexchangedtextchanged

Is it possible to change the event order?


Is it possible to change the order in which certain events are called? for instance, I have a ComboBox and when a selection is changed, I would like the SelectedIndexChanged event to be called before the TextChanged event is called. My honest opinion is that it is pretty stupid to call the TextChanged event before the SelectedIndexChanged event because it prevents me from knowing if the TextChanged event was called because a new item was selected.

Any help would be much appreciated.


Solution

  • No, you can't change the order - it's hard coded into the control code:

    // from http://referencesource.microsoft.com
    if (IsHandleCreated) { 
        OnTextChanged(EventArgs.Empty);
    } 
    
    OnSelectedItemChanged(EventArgs.Empty);
    OnSelectedIndexChanged(EventArgs.Empty); 
    

    If you have handlers for each event and need them to run in a certain order you could have the TextChanged event look for some indicator that the SelectedIndexChanged event has happened, then call the TextChanged handler from the SelectedIndexChanged handler, or just have SelectedIndexChanged do all the work.

    It depends on why you need them to run in a certain order.