i wanna select all by Ctrl + A. but it isn't possible.
yeah, actually it is possible. Don't be so quick to give up.
You want to make use of the 'KeyPress' event for the TextBox, then check to see if it is Ctrl+A. If it is, then use the SelectAll method built into the TextBox and mark the event as handled. If its not the Ctrl+A you wanted, then do nothing and the message will be passed along.
- private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
-
{
-
string bob = e.KeyChar.ToString();// Put a breakpoint here to see what each KeyChar is.
-
if (e.KeyChar == 1)
-
{
-
((TextBox)sender).SelectAll();
-
e.Handled = true;
-
}
-
}
-
Line 3 allows you to see what keypress is taking place. Just put a breakpoint here and every time you type a key while focus is on this control, you can look at the value of KeyPressEventArgs e. This is how I determined a value of '1' is Ctrl+A. A value of 106 would be a lower case j, for example.
Line 6 allows this to work for any textbox you direct to this function by way of its "KeyPress" event. Basically you are taking the object that triggered this event, casting it to a TextBox, then telling that object to .SelectAll();
Line 7 stops this KeyPress event from going any further. The Windows message is Handled.
Using the SelectAll() method doesn't have to be limited to the user actually pressing a key. You could call it when the text box receives focus if you like, so any time a user clicks on it or tabs to it, it automagically selects all the text.