469,293 Members | 1,364 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,293 developers. It's quick & easy.

C# - Clearing Event Queue?

jgroos
6
Here is the situation:

I have 25 textboxes that are all handled by the same TextChanged event handler. My problem is, when I click my "Clear" button on the form, it clears all the textboxes and then fires my TextChanged event handler for each of the 25 textboxes, because their text is being "changed". I know this is how it is supposed to work, but it makes clearing my form take WAY too long! :)

Is there any way I can clear the event queue so it doesn't do the TextChanged event handler for all of the textboxes?

Or is there another way to get around this situation?
May 18 '07 #1
5 9941
shidec
26
have u try to ignore code if Textbox is empty like this?

Expand|Select|Wrap|Line Numbers
  1. private void textBox1_TextChanged(object sender, EventArgs e)
  2. {
  3.     if (((TextBox)sender).Text != "")
  4.     {
  5.       ....
  6.       ....
  7.     }
  8. }
  9.  
May 18 '07 #2
jgroos
6
you are right, it does work, but it still clears very slowly because of the 25 calls to the eventhandler.

is there a way to "turn off" the event handler while i clear everything, and then turn it back on?
May 18 '07 #3
shidec
26
Ok, if u need to turn off/on event, u can write like this:
Expand|Select|Wrap|Line Numbers
  1. private void btnTurnOffEvent_Click(object sender, EventArgs e)
  2. {
  3.     this.textBox1.TextChanged -= new System.EventHandler(this.textBox1_TextChanged);
  4. }
  5.  
  6. private void btnTurnOnEvent_Click(object sender, EventArgs e)
  7. {
  8.     this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
  9. }
  10.  
May 18 '07 #4
jgroos
6
ahhh....perfect. thankyou!
NOW, is there a way to do this without writing this code for each individual textbox?....because I have 35 text boxes..... :)
May 18 '07 #5
shidec
26
of course, u dont need to writing the code,
just copy, paste 34 times, then edit it :p
hehehe

u can use foreach to iterate all of controls.
this program assume all of textbox in a form must be processed.
if u only need some textbox,
put those in a container like Panel then use poperty Controls of panel

Expand|Select|Wrap|Line Numbers
  1. private void btnTurnOffEvent_Click(object sender, EventArgs e)
  2. {
  3.     foreach (Control ctrl in Controls)
  4.     {
  5.         if (ctrl is TextBox)
  6.         {
  7.             ctrl.TextChanged -= new System.EventHandler(this.textBox1_TextChanged);
  8.         }
  9.     }    
  10. }
  11.  
  12. private void btnTurnOnEvent_Click(object sender, EventArgs e)
  13. {
  14.     foreach (Control ctrl in Controls)
  15.     {
  16.         if (ctrl is TextBox)
  17.         {
  18.             ctrl.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
  19.         }
  20.     }
  21. }
  22.  
May 18 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Graeme Matthew | last post: by
10 posts views Thread by jack | last post: by
9 posts views Thread by jeff | last post: by
5 posts views Thread by nt5515 | last post: by
8 posts views Thread by Brad Walton | last post: by
4 posts views Thread by tb2500 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.