Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
i have found this for selecting all the text in textbox by pressing ctrl+a for selecting all the text in textbox

C#
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
             if (e.KeyChar == '\x1')
            {
                ((TextBox)sender).SelectAll();
                e.Handled = true;
            }

        }


but the problem is i have many textbox in my data entry form. so i want to reduce the code. i have to write this code for every keypress event of textbox.
how can i do? writing one code for many textbox for selecting?
sorry for my bad english
Posted
Comments
ZurdoDev 8-Nov-12 8:47am    
Call me crazy but doesn't Ctrl+A always select all in a textbox. You don't need to write code for it.
shaikh-adil 8-Nov-12 8:52am    
i want ctrl + a in my software. so i want so i asked
ZurdoDev 8-Nov-12 8:55am    
But textboxes do it without you needing to write code.
jim lahey 8-Nov-12 9:00am    
I said this too. there's a wheel out there already but it needs reinventing for some reason.
shaikh-adil 8-Nov-12 9:00am    
no sir i am doing my project in windows form and in windows form textbox doesnt do it automatically

You can simply assign this handler to the KeyPress event of any TextBox you need. You've done the hard work by casting the sender parameter.

One question though, do you really need to do this? Ctrl + a is select all just about everywhere..
 
Share this answer
 
Comments
shaikh-adil 8-Nov-12 8:53am    
yup sir. i want this. but i dont think there isnt any solution
If you really need to handle the Ctrl+A, you wire key press event of text box to one event handler as shown below in form load.

C#
textBox1.KeyPress += new KeyPressEventHandler(CommonKeyPress);
textBox2.KeyPress += new KeyPressEventHandler(CommonKeyPress);


Then your function below should work great !

C#
private void CommonKeyPress(object sender, KeyPressEventArgs e)
{
  if (e.KeyChar == '\x1')
  {
    ((TextBox)sender).SelectAll();
    e.Handled = true;
  }
}


Milind
 
Share this answer
 
v2
Comments
shaikh-adil 8-Nov-12 9:03am    
awsome way of wiring dude.
Thanx for helping.
+5
MT_ 8-Nov-12 9:05am    
Glad it helped. Couldnt see the vote :(! If it helped, do mark it answer...Milind
bbirajdar 8-Nov-12 9:10am    
Never mind.. I will vote for the answer. It seems perfect to me +5
shaikh-adil 8-Nov-12 9:23am    
No overload for 'CommonKeyPress' matches delegate 'System.EventHandler' THIS ERROR IS COMMING???
MT_ 9-Nov-12 0:46am    
You got it working or still issue ?
C#
public void select all()
{
   if (e.KeyChar == '\x1')
            {
                ((TextBox)sender).SelectAll();
                e.Handled = true;
            }
}

private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
          selectall();
        }

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
          selectall();
        }

private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
          selectall();
        }
 
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