Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have many textbox ,now I want to bind all textChanged events to one
event handler ,I write something like this ,but I think it could be better
But HOW?...I meant a way by Delegates..
C#
txtbox1.TextChanged += new EventHandler(txt_TextChanged);
           txtbox2.TextChanged += new EventHandler(txt_TextChanged);
           txtbox3.TextChanged += new EventHandler(txt_TextChanged);
          .....


C#
void txt_TextChanged(object sender, EventArgs e)
       {
           errorProvider1.SetError((TextBox)(sender), "");
       }
Posted
Updated 17-Oct-10 20:27pm
v2

Steps
------
1. Write a TextChange event in code.
2. Open form designer.
3. Select required textboxes for which you want to share common TextChange event.
4. Open property window or press F4 (click event)
5. In TextChange event of property window, select TextChange event written by you in code.
6. Press enter.

No need to make code!
:)
 
Share this answer
 
v2
Comments
Dalek Dave 18-Oct-10 3:46am    
Good Answer.
Hi,

this should do the trick:

foreach (Control ctrl in this.Controls)
{  
  if (ctrl is TextBox)
  {
    ctrl.TextChanged += new EventHandler(txt_TextChanged);
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900