Is it possible to disable events?

O

ORC

Hi,

Is it possible to disable an event like e.g. disabling:
private void textBox1_TextChanged(object....) ?

Thanks,
Ole
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

You can remove all event handlers and in this case, when the event is fired,
nothing will happen.
Why would you need to disable an event in the first place?
 
N

Nicholas Paldino [.NET/C# MVP]

Ole,

Unless the class offers some sort of management for this, there is no
standard mechanism to do it.

Hope this helps.
 
S

Shiva

Not possible. But, to achieve this effect, just don't hook any event
handlers to that event.


Hi,

Is it possible to disable an event like e.g. disabling:
private void textBox1_TextChanged(object....) ?

Thanks,
Ole
 
O

ORC

Thank you all for your input!
The reason for disabling events is that if e.g. all form controls are
initialized with a value from a database during load, it can be fatal if a
changed event fires and calculates new values from some of the controls that
hasn't yet finished updating - this is e.g. what I experied. However i found
something usefull, but it demands that one will have to do some work
himself:


EventHandler radioButtonEvent;
radioButtonEvent = new
System.EventHandler(this.radioButton1_CheckedChanged);

And now the event can be turned on and off at will like this:
enabling the event:
radioButton1.CheckedChanged += radioButtonEvent;
disabling the event:
radioButton1.CheckedChanged -= radioButtonEvent;

Thanks,
Ole


Dmitriy Lapshin said:
Hi,

You can remove all event handlers and in this case, when the event is fired,
nothing will happen.
Why would you need to disable an event in the first place?

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

ORC said:
Hi,

Is it possible to disable an event like e.g. disabling:
private void textBox1_TextChanged(object....) ?

Thanks,
Ole
 
L

Lloyd Sheen

One thing you might want to try is the following:

You will find all the code to hook up the events in the generated code. You
can extract this code an put it into a subroutine that you can call in the
load after all the values required have been loaded.

One other thing is to set the controls to enabled = false. Then in the load
once the values are set enable all the controls. A disabled control will
not cause an event.

Lloyd Sheen

ORC said:
Thank you all for your input!
The reason for disabling events is that if e.g. all form controls are
initialized with a value from a database during load, it can be fatal if a
changed event fires and calculates new values from some of the controls
that
hasn't yet finished updating - this is e.g. what I experied. However i
found
something usefull, but it demands that one will have to do some work
himself:


EventHandler radioButtonEvent;
radioButtonEvent = new
System.EventHandler(this.radioButton1_CheckedChanged);

And now the event can be turned on and off at will like this:
enabling the event:
radioButton1.CheckedChanged += radioButtonEvent;
disabling the event:
radioButton1.CheckedChanged -= radioButtonEvent;

Thanks,
Ole


Dmitriy Lapshin said:
Hi,

You can remove all event handlers and in this case, when the event is fired,
nothing will happen.
Why would you need to disable an event in the first place?

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

ORC said:
Hi,

Is it possible to disable an event like e.g. disabling:
private void textBox1_TextChanged(object....) ?

Thanks,
Ole
 
O

ORC

A control that is disabled does indeed fire events! - the only solution is
to remove the eventhandler!

Thanks,
Ole

Lloyd Sheen said:
One thing you might want to try is the following:

You will find all the code to hook up the events in the generated code. You
can extract this code an put it into a subroutine that you can call in the
load after all the values required have been loaded.

One other thing is to set the controls to enabled = false. Then in the load
once the values are set enable all the controls. A disabled control will
not cause an event.

Lloyd Sheen

ORC said:
Thank you all for your input!
The reason for disabling events is that if e.g. all form controls are
initialized with a value from a database during load, it can be fatal if a
changed event fires and calculates new values from some of the controls
that
hasn't yet finished updating - this is e.g. what I experied. However i
found
something usefull, but it demands that one will have to do some work
himself:


EventHandler radioButtonEvent;
radioButtonEvent = new
System.EventHandler(this.radioButton1_CheckedChanged);

And now the event can be turned on and off at will like this:
enabling the event:
radioButton1.CheckedChanged += radioButtonEvent;
disabling the event:
radioButton1.CheckedChanged -= radioButtonEvent;

Thanks,
Ole


in message news:[email protected]...
Hi,

You can remove all event handlers and in this case, when the event is fired,
nothing will happen.
Why would you need to disable an event in the first place?

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Hi,

Is it possible to disable an event like e.g. disabling:
private void textBox1_TextChanged(object....) ?

Thanks,
Ole
 
B

Bruce Wood

There is another solution that I use which, while it's less elegant,
is easy to understand, implement, and maintain, which makes it a
winner in my books.

Simply create a private bool flag in your form:

private bool populatingDisplay = false;

Then inside each event handler you say:

private void txtMyTextBox_TextChanged(object sender, System.EventArgs
e)
{
if (!this.populatingDisplay)
{
... react to event here ...
}
}

Now whenever you're mucking with the controls on the form and don't
want to react to the resulting events, just do this:

this.populatingDisplay = true;
.... do whatever you like with the controls on the form ...
this.populatingDisplay = false;

The events will still fire, but your bool flag will cause them all to
be ignored. (Yes, I hate bool flags, too, but this is one of those
cases in which all other solutions are uglier still.)
 
O

ORC

You're right - your solution is a lot easier to understand and maintain -
Thanks, I will use that instead!

Ole
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top