By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

Is it possible to disable events?

P: n/a
ORC
Hi,

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

Thanks,
Ole
Nov 16 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Ole,

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

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ORC" <or*@sol.dk> wrote in message
news:u0**************@TK2MSFTNGP15.phx.gbl...
Hi,

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

Thanks,
Ole

Nov 16 '05 #2

P: n/a
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" <or*@sol.dk> wrote in message
news:u0**************@TK2MSFTNGP15.phx.gbl...
Hi,

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

Thanks,
Ole


Nov 16 '05 #3

P: n/a
Not possible. But, to achieve this effect, just don't hook any event
handlers to that event.
"ORC" <or*@sol.dk> wrote in message
news:u0**************@TK2MSFTNGP15.phx.gbl...
Hi,

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

Thanks,
Ole

Nov 16 '05 #4

P: n/a
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_CheckedChang ed);

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 [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:Oy**************@TK2MSFTNGP15.phx.gbl...
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" <or*@sol.dk> wrote in message
news:u0**************@TK2MSFTNGP15.phx.gbl...
Hi,

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

Thanks,
Ole

Nov 16 '05 #5

P: n/a
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" <or*@sol.dk> wrote in message
news:O%****************@TK2MSFTNGP14.phx.gbl...
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_CheckedChang ed);

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 [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:Oy**************@TK2MSFTNGP15.phx.gbl...
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" <or*@sol.dk> wrote in message
news:u0**************@TK2MSFTNGP15.phx.gbl...
> Hi,
>
> Is it possible to disable an event like e.g. disabling:
> private void textBox1_TextChanged(object....) ?
>
> Thanks,
> Ole
>
>


Nov 16 '05 #6

P: n/a
ORC
A control that is disabled does indeed fire events! - the only solution is
to remove the eventhandler!

Thanks,
Ole

"Lloyd Sheen" <sq*******************@tostopspamhotmail.com> wrote in message
news:yJ********************@rogers.com...
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" <or*@sol.dk> wrote in message
news:O%****************@TK2MSFTNGP14.phx.gbl...
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_CheckedChang ed);

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 [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote in message news:Oy**************@TK2MSFTNGP15.phx.gbl...
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" <or*@sol.dk> wrote in message
news:u0**************@TK2MSFTNGP15.phx.gbl...
> Hi,
>
> Is it possible to disable an event like e.g. disabling:
> private void textBox1_TextChanged(object....) ?
>
> Thanks,
> Ole
>
>



Nov 16 '05 #7

P: n/a
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.)
Nov 16 '05 #8

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

Ole
"Bruce Wood" <br*******@canada.com> wrote in message
news:a6**************************@posting.google.c om...
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.)

Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.