Click here to Skip to main content
15,906,625 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have many textbox ,now I want to bind all textChanged events to one
event handler ,is it possible to declare an object to handle all textChanged events?
I mean something like this :

(TextBox).TextChanged += new EventHandler(txt_TextChanged);




please tell me HOW?
Posted

Hi,

You can write one method to handle multiple textbox's textchanged method.

EventHandler takes the name of the method to be associated with the event. Hence if you mention same name for multiple textchanged, it would work.

TextBox1.TextChanged += new EventHandler(txt_TextChanged);
TextBox2.TextChanged += new EventHandler(txt_TextChanged);
TextBox3.TextChanged += new EventHandler(txt_TextChanged);


Above code shows how 3 textbox(TextBox1,TextBox2,TextBox3) having same event handler method (txt_TextChanged).

Hope that helps,

Thanks,
Arindam D Tewary
 
Share this answer
 
Comments
Arindam Tewary 19-Oct-10 4:21am    
Any reason for marking this as "Bad" answer??
Rajesh Anuhya 20-Oct-10 2:47am    
i think there is nothing wrong in "Arindam D Tewary" answer..,
Nima.naqipoor 11-Apr-12 2:46am    
sorry that was just a mistake!
OK you already know how to add a event handler to the TextChanged event and I'm guessing you also know that if you specify the same method name in this case txt_TextChanged then all the then they would use the same method to process the TextChanged event, such as;
C#
void txt_TextChanged(object sender, EventArgs e)
{
    //This becomes a reference to the TextBox that raised the TextChanged event
    TextBox textBox = sender as TextBox;

}


If you want to use a separate class to process all TextChanged events you can also do that.
If you create a class like the following (remember to add using System.Windows.Forms; to the class);
C#
public class TextChangeHandler
{
    public void TextChanged(object sender, EventArgs e)
    {
        //This becomes a reference to the TextBox that raised the TextChanged event
        TextBox textBox = sender as TextBox;
    }
}

you can then use the following to handle the TextChanged event;
C#
//This needs to be global for the form
TextChangeHandler txtChangeHandler = new TextChangeHandler();

//These go in the form constructor
textBox1.TextChanged += new EventHandler(txtChangeHandler.TextChanged);
textBox2.TextChanged += new EventHandler(txtChangeHandler.TextChanged);
textBox3.TextChanged += new EventHandler(txtChangeHandler.TextChanged);


You can also make the class static and use;
C#
public static class TextChangeHandler
{
    public static void TextChanged(object sender, EventArgs e)
    {
        //This becomes a reference to the TextBox that raised the TextChanged event
        TextBox textBox = sender as TextBox;
    }
}

you can then use the following to handle the TextChanged event;
C#
textBox1.TextChanged += new EventHandler(TextChangeHandler.TextChanged);
textBox2.TextChanged += new EventHandler(TextChangeHandler.TextChanged);
textBox3.TextChanged += new EventHandler(TextChangeHandler.TextChanged);
 
Share this answer
 
Try this:

foreach (Control c in this.Controls)
{
    ((TextBox)c).TextChanged += new EventHandler(Form1_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