Allowing Enter or Return in controls such as TextBox when using Windows Forms .NET 2.0
Posted by jimblackler on Mar 6, 2008
Many applications feature text input boxes that used to accept input for immediate use by the program. For instance, a program might provide a dialog allowing user to search for text in a document. This would feature a text box to enter the text, and a button next to it labelled ‘search’ that would immediately act on the supplied text.
However many users will not want to click the ‘search’ button, but simply press Return or Enter after entering the text.
How can this be done in a C# .NET 2.0 Windows Forms application? One way is to set the Accept property in the parent form to refer to the ‘search’ button. This will make Enter / Return automatically press the button, when pressed in any child control on the form.
This doesn’t always offer the level of control the application developer wants. Any control could activate the press, not just the relevant text box. It requires a button, which might not always be desirable. Also it is not trivial to allow multiple textboxes on a single form that all accept Enter / Return in that way.
The best way that I have found is to add a KeyDown event to the textbox and compare the pressed key with Keys.Enter. Then, act on that key and set SuppressKeyPress in the event arguments so that the system does not try to do anything further with the keypress. (Typically what happens otherwise is that an ‘error’ beep is played).
- private void textBox1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.None)
- {
- // do something with textBox1.Text
- e.SuppressKeyPress = true;
- }
- }
However if you implement that code and run the application, you’ll find nothing happens when you press Enter. The reason is that the Enter key press never reaches the KeyDown event. This happens because certain key presses are reserved for system use. The solution is to supply a PreviewKeyDown event in the textbox. This gives the opportunity to override the system override as it were, and allow the key to reach the KeyDown even in the textbox.
- private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
- {
- if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.None)
- {
- e.IsInputKey = true;
- }
- }
I’ve provided source for an example application as a zip file. Requires Visual Studio 2005 or later, C# express will also work.
Hello Jim, well I really dont know about “certain key presses are reserved for system use” you said above.
the following works :
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter)==true)
{
MessageBox.Show(“Hey this works”);
}
}
If you can see that am giving an option ‘OR’ for Enter/Return keys. As system reads this Enter Key as Enter sometimes and Return sometimes and this also depends on OS/Regional Settings etc.
I feel no need to override system using previewKeyDown event.
Thanks.
It’s possible it depends on the type of dialog, the other controls on the dialog, the version of the framework and the version of the operating system.
thanks for the post it solve my problem thanks again
Nice! This worked