Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable event handler in c# on textchanged

Tags:

c#

events

handler

I want to be able to disable or enable textchanged event when I need to. I have made my function, but I need to dismiss event handler, how can I do that?

Here is my code:

private void textBox1_TextChanged(object sender, EventArgs e)
{
           //something
}
like image 1000
user123_456 Avatar asked May 17 '12 10:05

user123_456


2 Answers

This to add the event

textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged);

this to remove the event

textBox1.TextChanged -= new TextChangedEventHandler(textBox1_TextChanged);

Or just the method name

This to add the event

textBox1.TextChanged += textBox1_TextChanged;

this to remove the event

textBox1.TextChanged -= textBox1_TextChanged;

Hope it helps.

like image 118
hjgraca Avatar answered Oct 06 '22 01:10

hjgraca


simply un-register the event

 yourEvent-= YourFunction

and if you want to register again

 yourEvent+= YourFunction
like image 25
Sleiman Jneidi Avatar answered Oct 06 '22 01:10

Sleiman Jneidi