how do I disable events for controls on a form?

  • Thread starter Thread starter Scott Emick
  • Start date Start date
S

Scott Emick

How can I disable events for the controls on a form? I tried setting the
form's enable property to false, but that doesn't stop events from firing on
its controls.

I need to temporarily disable a form and load values into controls without
any events firing, then re-enable the form.
 
I need to temporarily disable a form and load values into controls
without any events firing, then re-enable the form.

You can use AddHandler/RemoveHandler to add/remove events dynamically.
 
I suspect you could do it with reflection by scanning for delegates attached
to each control's events, recording them, removing them, and then adding
them back again later. But do you really need to do that? For most
applications that's like shooting a squirrel with a shotgun. Might even be
considered bad design. I'm 90% sure you can get away with something simpler:

1. Load values into the controls before they're added to the form.
2. Declare a boolean variable IgnoreEvents and check it in all relevant
event-handling code.
3. Manually add and remove event handlers with AddHandler and RemoveHandler.

The first is ideal because it's the simplest but it's not always feasible. I
would lean towards #2 because it makes your code more explicitly stateful:
while debugging, the flow of execution to always hit the handler, you'll
always be able to tell when an event was fired, without having check if the
handler's been removed. What if something goes wrong and the handler isn't
put back? That's a bit harder to track down than just seeing that a variable
'IgnoreEvents' is still set to True. Make IgnoreEvents a Property and you'll
also be able to break on when it's set to a new value.

Bob
 
Scott Emick said:
How can I disable events for the controls on a form? I tried setting the
form's enable property to false, but that doesn't stop events from firing on
its controls.

Have you tried setting each control's Enabled property?

LFS
 
Scott Emick said:
How can I disable events for the controls on a form? I
tried setting the form's enable property to false, but that doesn't
stop events from firing on its controls.

Add the handlers to the controls' events after 'InitializeComponent' using
'AddHandler'. Notice that you will have to remove the 'Handles...' after
the event handlers added by VS.NET in order to prevent the handlers from
being associated with the events when the controls are instantiated.
 
I have the same problem. When I load a form, in the load event, I want to
initialize some text boxes with varying text. However, in the
textbox_Changed event, I also have code to process the new text and this
event fires evertime I change the text box contents. Setting a variable to
disable the changed event won't work because you have to reset before the
load event routine finishes in order to have the changed event go thru it's
processing. However, the textbox_changed events fire after the load event
compeletes. It's a catch 22 situation and I also would like some advice on
how to work this problem.
 
compeletes. It's a catch 22 situation and I also would like some advice on
how to work this problem.

Bob's suggestion of using a boolean to IgnoreEvents would work. In the
load event of the form, at the top, set IgnoreEvents = True then inside
each of the event handlers, check that variable and if set, exit the
handler instead of processing. Then, at the end of the load event, set
IgnoreEvents back to False.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Back
Top