locked
Switching a state of control's event in C# RRS feed

  • Question

  • Hello,

    In my application I refill ComboBox control sometimes. A problem is that there is an event "SelectedItemChanged" to this ComboBox and after each refiling it jumps to this event. Firstly I changed "cbControl.ItemsSource= " to "foreach..." and "cbControl.Items.Add()". This helped, but not for a long time, because I had to add "cbControl.Items.Clear()" that somehow generated the same jumps to the "SelectedItemChanged" event.

    Is there any possibility to temporarily forbid an event and then to allow it?

    Thanks.

    Saturday, May 14, 2011 6:40 PM

Answers

  • You can unsubscribe to an event and then subscribe back.

     

    eg. cbControl.SelectedItemChanged -= new cbControl_SelectedItemChanged(object Sender, EventArgs e);

         //Make the necessary function calls here

         cbControl.SelectedItemChanged += new cbControl_SelectedItemChanged(object Sender, EventArgs e);

    • Marked as answer by leozel Saturday, May 14, 2011 9:45 PM
    Saturday, May 14, 2011 7:07 PM

All replies

  • You can unsubscribe to an event and then subscribe back.

     

    eg. cbControl.SelectedItemChanged -= new cbControl_SelectedItemChanged(object Sender, EventArgs e);

         //Make the necessary function calls here

         cbControl.SelectedItemChanged += new cbControl_SelectedItemChanged(object Sender, EventArgs e);

    • Marked as answer by leozel Saturday, May 14, 2011 9:45 PM
    Saturday, May 14, 2011 7:07 PM
  • It works, thanks a lot!

    Just for accuracy:

    cbControl.SelectionChanged -=

    new SelectionChangedEventHandler(cbControl_SelectionChanged);

    and then:

    cbControl.SelectionChanged +=new SelectionChangedEventHandler

    (cbControl_SelectionChanged);

     

     

     

     

     

     

     

     

     


    Saturday, May 14, 2011 9:43 PM
  • Right, Sorry about the wrong syntax, it was pretty late in the night ;)

     

    Glad to help

    Sunday, May 15, 2011 1:08 AM