Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi evereyone, i have a little question:
I have a form with ten textboxes and i want to handle the onKeyPress events for all the textboxes in just one method without need to write one method for every textbox.

Example:
I don't want to have something like this:
private void textBox01_OnKeyPress() {}
       private void textBox02_OnKeyPress() {}
       private void textBox03_OnKeyPress() {}
       ........
       private void textBox10_OnKeyPress() {}

I need something like this:
private void allTextBoxes_OnKeyPress() {}

With just a single method control the OnKeyPress event of all TextBoxes.

How can i do this?
It is possible?

Thanks in advance.
Best Regards.
Posted
Updated 14-Dec-10 2:38am
v4

You can do it like this:
TextBox textBox = null;
foreach (Control control in this.Controls)
{
    if ((textBox = control as TextBox) != null)
    {
        textBox.KeyPress += new KeyPressEventHandler(textBox_KeyPress);
    }
}
 
Share this answer
 
v2
Comments
Pablinff 13-Dec-10 19:50pm    
It seems to be a good solution can you explain a little more how to implement it?
Aparently you are creating the event handler, then just need to define the method textBox_KeyPress.
where i define that event handler?
thanks!
Toniyo Jackson 14-Dec-10 8:38am    
replace productsearch with ProductSearch .
Hiren solanki 14-Dec-10 8:48am    
@TonyoJackson : You are in wrong direction, you've put a comment for another question here. See the question before posting comment.
As d@nish has said you can do it programmatically using his code.

To do the same thing using the designer simply select all the TextBoxes on your Form, select the Events tab on the Properties Window, select the KeyPress item and either: type in a name that you want to use and then hit Return, or double click it to use the default method name (warning this will use the name of the first TextBox you selected e.g. textBox1_KeyPress(....)).
 
Share this answer
 
v2
So just assign the same event handler method to the OnKeyPress event for each TextBox.

EDIT ==============

In response to your comment, the above is EXACTLY what you want. Create a single handler:

private void allTextBoxesKeyPress(...)

and use THAT handler for ALL of the text box controls.

 
Share this answer
 
v3
Comments
Pablinff 13-Dec-10 18:29pm    
Hi John, thats's exactly what i don't want to do.
I don't want to have something like this:
private void textBox01_OnKeyPress() {}
private void textBox02_OnKeyPress() {}
private void textBox03_OnKeyPress() {}
........
private void textBox10_OnKeyPress() {}

I need something like this:
private void allTextBoxes_OnKeyPress() {}
With just a single method control the OnKeyPress event of all TextBoxes.

Thanks!

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