Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i have found this for selecting all the text in textbox by pressing ctrl+a for selecting all the text in textbox
 
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 8-Nov-12 2:43am
Comments
ryanb31 at 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 at 8-Nov-12 8:52am
   
i want ctrl + a in my software. so i want so i asked
ryanb31 at 8-Nov-12 8:55am
   
But textboxes do it without you needing to write code.
jim lahey at 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 at 8-Nov-12 9:00am
   
no sir i am doing my project in windows form and in windows form textbox doesnt do it automatically
jim lahey at 8-Nov-12 9:07am
   
Yes it does, I just tried it myself.
ryanb31 at 8-Nov-12 9:07am
   
Then something is broken. They do select all by default.
shaikh-adil at 8-Nov-12 9:13am
   
i am using vs 2010 and in win xp i dont know how you can do this shortcut. It is not available here. Is it was there so why i didint try that?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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..
  Permalink  
Comments
shaikh-adil at 8-Nov-12 8:53am
   
yup sir. i want this. but i dont think there isnt any solution
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
 
 textBox1.KeyPress += new KeyPressEventHandler(CommonKeyPress);
 textBox2.KeyPress += new KeyPressEventHandler(CommonKeyPress);
 
Then your function below should work great !
 
private void CommonKeyPress(object sender, KeyPressEventArgs e)
{
  if (e.KeyChar == '\x1')
  {
    ((TextBox)sender).SelectAll();
    e.Handled = true;
  }
}
 
Milind
  Permalink  
v2
Comments
shaikh-adil at 8-Nov-12 9:03am
   
awsome way of wiring dude.
Thanx for helping.
+5
Milind Thakkar at 8-Nov-12 9:05am
   
Glad it helped. Couldnt see the vote :(! If it helped, do mark it answer...Milind
aspnet_regiis -i at 8-Nov-12 9:10am
   
Never mind.. I will vote for the answer. It seems perfect to me +5
shaikh-adil at 8-Nov-12 9:23am
   
No overload for 'CommonKeyPress' matches delegate 'System.EventHandler' THIS ERROR IS COMMING???
Milind Thakkar at 9-Nov-12 0:46am
   
You got it working or still issue ?
Milind Thakkar at 8-Nov-12 9:07am
   
Do upvote if it helped..Thanks
shaikh-adil at 8-Nov-12 9:08am
   
vote
Lolz sorry i am from my handset so i cant vote now.
Deffinetly Will do it from my pc
:)
shaikh-adil at 9-Nov-12 3:58am
   
i had wired up the event in form load sir.
But that error is comming can you suggest some more step. Plz help
Milind Thakkar at 9-Nov-12 4:09am
   
No worries. Check the updated solution. My bad, little wrong event handler name. I have tested this code and its working now. Cheers - Milind
shaikh-adil at 9-Nov-12 9:47am
   
thank you sir
thank you very much for helping
shaikh-adil at 10-Nov-12 1:04am
   
sir one more question if i have diffrent diffrent method which will run for only one event. For example i have a method ex. Validating event of all the textbox. Then can i decrease code?
I have validation() validation1() and validation2() this will run on diffrent textbox but on same event ex. Textbox1Mousehover textbox2 mousehover textbox3mousehover so how can i do to decrease the code lenght
sariqkhan at 10-Nov-12 1:51am
   
nice one
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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();
        }
 
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 205
1 Sergey Alexandrovich Kryukov 185
2 jlopez788 144
3 Rob Philpott 100
4 Prakriti Goyal 97
0 OriginalGriff 6,837
1 Sergey Alexandrovich Kryukov 5,559
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
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