2

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.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
MJLaukala
  • 171
  • 1
  • 3
  • 13
  • The short answer is no, you cannot change the order of events fired in .NET controls. But maybe you don't need to. What is your use case? That is, what are you trying to do within `TextChanged` such that you need to know if `SelectedIndexChanged` has fired? – ean5533 Nov 01 '12 at 21:46
  • But the text was changed... that's why it's called. Doesn't seem stupid at all. – Simon Whitehead Nov 01 '12 at 21:49
  • The text was changed _because another item was selected_. It is stupid to tell it in the wrong order because it cannot be understood. – ygoe Apr 24 '14 at 14:09

3 Answers3

3

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.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

There is something you can do but the solution is not a good one & you will surely confuse any one who might maintain your application in the future and maybe yourself as well once you forget what you've done. But here it is anyway:

The idea is to have the same function handle both events, keep track of the old values of index & text so that you can order how the events are handled accordingly

// two fields to keep the previous values of Text and SelectedIndex
private string _oldText = string.Empty;
private int _oldIndex = -2;
.
// somewhere in your code where you subscribe to the events
this.ComboBox1.SelectedIndexChanged += 
new System.EventHandler(ComboBox1_SelectedIndexChanged_AND_TextChanged);
this.ComboBox1.TextChanged+= 
new System.EventHandler(ComboBox1_SelectedIndexChanged_AND_TextChanged);
.
.

/// <summary>
///  Shared event handler for SelectedIndexChanged and TextChanged events.
///  In case both index and text change at the same time, index change
///  will be handled first.
/// </summary>
private void ComboBox1_SelectedIndexChanged_AND_TextChanged(object sender, 
        System.EventArgs e)
{

   ComboBox comboBox = (ComboBox) sender;

   // in your case, this will execute on TextChanged but 
   // it will actually handle the selected index change
   if(_oldIndex != comboBox.SelectedIndex) 
   {
      // do what you need to do here ...   

      // set the current index to this index 
      // so this code doesn't exeute again
      oldIndex = comboBox.SelectedIndex;
   }
   // this will execute on SelecteIndexChanged but
   // it will actually handle the TextChanged event
   else if(_oldText != comboBox.Test) 
   {
      // do what you need to ...

      // set the current text to old text
      // so this code doesn't exeute again 
      _oldText = comboBox.Text;      
   }

}

Note that this code will still work when the events are fired separately - only text changes or only index changes.

user1416420
  • 498
  • 2
  • 7
0
if ( SelectedIndex == -1 )  // only the text was changed.