【问题标题】:Is it possible to change the event order?是否可以更改活动顺序?
【发布时间】:2012-11-01 21:42:33
【问题描述】:

是否可以更改某些事件的调用顺序? 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.老实说,在 SelectedIndexChanged 事件之前调用 TextChanged 事件是非常愚蠢的,因为它使我无法知道是否因为选择了新项目而调用了 TextChanged 事件。

任何帮助将不胜感激。

【问题讨论】:

  • 简短的回答是否定的,您不能更改在 .NET 控件中触发的事件的顺序。但也许你不需要。你的用例是什么?也就是说,你想在TextChanged 中做什么,这样你就需要知道SelectedIndexChanged 是否被解雇了?
  • 但是文本被改变了......这就是它被调用的原因。看起来一点也不傻。
  • 文本已更改因为选择了另一个项目。以错误的顺序告诉它是愚蠢的,因为它无法理解。

标签: c# events combobox selectedindexchanged textchanged


【解决方案1】:

不,您不能更改顺序 - 它被硬编码到控制代码中:

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

OnSelectedItemChanged(EventArgs.Empty);
OnSelectedIndexChanged(EventArgs.Empty); 

如果您有每个事件的处理程序并需要它们按特定顺序运行,您可以使用 TextChanged 事件查找SelectedIndexChanged 事件已发生的一些指示符,然后从SelectedIndexChanged 处理程序调用TextChanged 处理程序,或者让SelectedIndexChanged 完成所有工作。

这取决于为什么您需要它们按特定顺序运行。

【讨论】:

    【解决方案2】:

    您可以做一些事情,但解决方案不是一个好的解决方案,而且一旦您忘记了自己所做的事情,您肯定会混淆任何可能在未来维护您的应用程序的人,甚至可能还有您自己。但无论如何它都在这里:

    这个想法是让 same 函数处理这两个事件,跟踪索引和文本的旧值,以便您可以订购相应的事件处理方式

    // 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;      
       }
    
    }
    

    请注意,当事件单独触发时,此代码仍然有效 - 仅更改文本或仅更改索引。

    【讨论】:

      【解决方案3】:
      if ( SelectedIndex == -1 )  // only the text was changed.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-06
        • 1970-01-01
        • 1970-01-01
        • 2019-06-09
        • 2010-09-12
        • 2020-07-30
        • 2021-10-20
        • 2017-01-06
        相关资源
        最近更新 更多