Click here to Skip to main content
Sign Up to vote bad
good
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 - 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
ryanb31 - 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
jim lahey - 8-Nov-12 9:07am
Yes it does, I just tried it myself.
ryanb31 - 8-Nov-12 9:07am
Then something is broken. They do select all by default.
shaikh-adil - 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?

3 solutions

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  
Comments
shaikh-adil - 8-Nov-12 9:03am
awsome way of wiring dude. Thanx for helping. +5
Milind Thakkar - 8-Nov-12 9:05am
Glad it helped. Couldnt see the vote :(! If it helped, do mark it answer...Milind
aspnet_regiis -i - 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???
Milind Thakkar - 9-Nov-12 0:46am
You got it working or still issue ?
Milind Thakkar - 8-Nov-12 9:07am
Do upvote if it helped..Thanks
shaikh-adil - 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 - 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 - 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 - 9-Nov-12 9:47am
thank you sir thank you very much for helping
shaikh-adil - 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 - 10-Nov-12 1:51am
nice one +5
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  
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 - 8-Nov-12 8:53am
yup sir. i want this. but i dont think there isnt any solution

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Christian Graus 544
1 Ron Beyer 296
2 OriginalGriff 258
3 samadhan_kshirsagar 229
4 Sergey Alexandrovich Kryukov 183
0 Sergey Alexandrovich Kryukov 7,007
1 Prasad_Kulkarni 3,815
2 OriginalGriff 3,557
3 _Amy 3,372
4 CPallini 2,975


Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid