I have a Windows forms application that has a standard TextBox on it. There are no events being handled and no menus on the form. When I press the Ctrl+A shortcut to select all the text, I get a ding, and nothing is selected.
To confirm I didn't inadvertently code something I created a new Windows forms application with only a textbox on the form. I tried it with both C# and VB.Net and it is the same in both. I have tried this in Visual Studio 2012 Update 1 running on Windows 7, and Visual Studio 2008 running on Windows XP and it behaves the same in each instance.
I can catch the key stroke combination in the KeyDown event easily enough, but even after setting e.Cancel = true
the machine stills sounds the "ding".
Is there a way to suppress the sound, or even better, a way to make the textbox accept the shortcut the same way a non-multiline textbox does?
Examples. The following code example uses TextBox, a derived class, to create a multiline TextBox control with vertical scroll bars. This example also uses the AcceptsTab, AcceptsReturn, and WordWrap properties to make the multiline text box control useful for creating text documents.
In Windows forms, TextBox plays an important role. With the help of TextBox, the user can enter data in the application, it can be of a single line or of multiple lines. In TextBox, you are allowed to create a multiline TextBox which stores multiple lines of the content using Multiline property of the TextBox.
A multiline text box allows you to display more than one line of text in the control. If the WordWrap property is set to true , text entered into the multiline text box is wrapped to the next line in the control.
This is a surprise to many programmers but the native Windows edit control doesn't actually implement Ctrl+A as a shortcut when it is in multi-line mode. It has to be implemented by the program that uses it. You can see this for example in Notepad, a program that uses a multi-line edit control. Use File + Open + File, select c:\windows\notepad.exe, open the Accelerator node and double-click one of the tables.
Implementing it is not difficult:
private void textBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == (Keys.Control | Keys.A)) {
textBox1.SelectAll();
e.Handled = e.SuppressKeyPress = true;
}
}
UPDATE: changed in .NET 4.6.1, System.Windows.Forms.TextBox now implements Ctrl+A for multi-line textboxes as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With