Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i m having a coding
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
       {
           if (((int)e.KeyChar >= 65 && (int)e.KeyChar <= 90) || ((int)e.KeyChar >= 97 && (int)e.KeyChar <= 122) || ((int)e.KeyChar) == 8 || ((int)e.KeyChar) == 32 || ((int)e.KeyChar) == 46)
           {
               e.Handled = false;
               errorProvider1.SetError(textBox1, "");
           }
           else
           {
               e.Handled = true;
               errorProvider1.SetError(textBox1, "Invalid Character");
           }
       }


like this and i m going to use same code in textbox2,textbox3,textbox4 so i dont want to write again and again... i need to write once and use tat many times... how can i do that...
Posted

U want to add the method textBox1_KeyPress to KeyPress eventhandler of other textboxes as well.

U can do this

1. using the GUI builder in Visual Studio:

Select all the appropriate textboxes in the GUI builder (by holding down the control key). Then in the properties window, click on the small lightning icon. Navigate down to keyPress event and click the down arrow in front of it. There u will find this method named textBox1_KeyPress. Click on it.

OR

2. using code:

write this somewhere in your Form1_Load(object sender, EventArgs e) or Form1() method:
textBox2.KeyPress+=textBox1_KeyPress;
textBox3.KeyPress+=textBox1_KeyPress;
textBox4.KeyPress+=textBox1_KeyPress;

and so on...


Actually if You use method 1, the code in method 2 automatically gets written in InitializeComponent() method in Form1.Designer.cs file.

*Replace "Form1" by name of the form in question.
 
Share this answer
 
v2
Comments
selva_1990 30-Dec-12 10:13am    
can u explain second method... its urgent
Alan N 30-Dec-12 10:19am    
There is a hard coded reference to textbox1 in the event handler method which will need modifying to use the specific control passed in via the sender parameter.
i.e. TextBox tb = (TextBox)sender;

and then errorProvider1.SetError(tb, ""); etc...
selva_1990 30-Dec-12 10:33am    
thank u bro... execellent
I would have done it by refactoring the chunk of reusable code in a common method. Hope that too works.
C#
private static void commonMethod(KeyPressEventArgs e, TextBox textBox)
    {
        if (((int)e.KeyChar >= 65 && (int)e.KeyChar <= 90) || ((int)e.KeyChar >= 97 && (int)e.KeyChar <= 122) || ((int)e.KeyChar) == 8 || ((int)e.KeyChar) == 32 || ((int)e.KeyChar) == 46)
        {
            e.Handled = false;
            errorProvider1.SetError(textBox, "");
        }
        else
        {
            e.Handled = true;
            errorProvider1.SetError(textBox, "Invalid Character");
        }
    }



Thanks
windson,


For Jobs and entertainment visit www.bachelorpost.com
 
Share this answer
 
v3
Comments
selva_1990 30-Dec-12 10:28am    
can u elaborate i m a beginner
Pavan Navule 30-Dec-12 10:48am    
See my updated answer
selva_1990 30-Dec-12 10:58am    
thank u :)so much
Pavan Navule 30-Dec-12 11:03am    
You are welcome.

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