Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change already attached event handler runtime

I have created a TextBox dynamically and attached a Tap event handler to it using:

control.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(OnClick1);

It works fine. But, now I want to change the event handler to point to some different method. I tried:

control.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(OnClick2);

But, it still points to first event handler. i.e. OnClick1. What can I do to make it point OnClick2? Also is there a way to remove this event handler completely?

like image 860
Rajesh Sonar Avatar asked Nov 22 '11 11:11

Rajesh Sonar


1 Answers

You need to remove the first handler first:

control.Tap -= OnClick1;
control.Tap += OnClick2;

(Note the rather simpler use of method group conversions, instead of explicitly creating the event handler. It does the same thing, but is much more readable.)

like image 73
Jon Skeet Avatar answered Sep 20 '22 17:09

Jon Skeet