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

how do I disable events for controls on a form?

P: n/a
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.

--
Scott Emick
Web Programmer
Fox International
Remove the ham from mail address if it's not spam
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Scott Emick" <se****@ham.fox-international.com> wrote in
news:#p**************@TK2MSFTNGP15.phx.gbl:
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.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #2

P: n/a
Bob
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" <se****@ham.fox-international.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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.

--
Scott Emick
Web Programmer
Fox International
Remove the ham from mail address if it's not spam

Nov 21 '05 #3

P: n/a

"Scott Emick" <se****@ham.fox-international.com> wrote
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
Nov 21 '05 #4

P: n/a
"Scott Emick" <se****@ham.fox-international.com> schrieb:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #5

P: n/a
Scott,

To help you to decide I go for the answer from Lucas

Cor
Nov 21 '05 #6

P: n/a
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.

"Scott Emick" wrote:
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.

--
Scott Emick
Web Programmer
Fox International
Remove the ham from mail address if it's not spam

Nov 21 '05 #7

P: n/a
On Wed, 6 Oct 2004 14:27:01 -0700, Dennis wrote:
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.
Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.