Click here to Skip to main content
12,444,360 members (53,966 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
i m having a coding
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 30-Dec-12 3:34am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

I would have done it by refactoring the chunk of reusable code in a common method. Hope that too works.
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
  Permalink  
v3
Comments
selva_1990 30-Dec-12 10:28am
   
can u elaborate i m a beginner
widnson 30-Dec-12 10:48am
   
See my updated answer
selva_1990 30-Dec-12 10:58am
   
thank u :)so much
widnson 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 30 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100