.NET/C#/WinForms: small code snippet to enable Ctrl-A for select all in a single/multi-line TextBox
Posted by jpluimers on 2012/12/04
WinForms does not automatically enable Ctrl-A as “Select All” action.
The below code snippet works when you bind it to the KeyDown event of a TextBox (actually the event is on Control).
The e.SuppressKeyPress = true suppresses the bell sound in a multiline TextBox, as e.Handled = true won’t.
private void textBox_KeyDown_HandleCtrlAToSelectAllText(object sender, KeyEventArgs e)
{
TextBox textBox = sender as TextBox;
if (null != textBox)
{
if (e.Control && e.KeyCode == Keys.A)
{
textBox.SelectAll();
e.SuppressKeyPress = true;
}
}
}
–jeroen






Leave a comment