Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

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.

if ( SelectedIndex == -1 )  // only the text was changed.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top