What is definitively the best way of performing an action based on the user's input of the Enter key (Keys.Enter
) in a .NET TextBox
, assuming ownership of the key input that leads to suppression of the Enter key to the TextBox itself (e.Handled = true)?
Assume for the purposes of this question that the desired behavior is not to depress the default button of the form, but rather some other custom processing that should occur.
Add a keypress event and trap the enter key
Programmatically it looks kinda like this:
Then Add a handler in code...
Inorder to link the function with the key press event of the textbox add the following code in the designer.cs of the form:
Now define the function 'OnKeyDownHandler' in the cs file of the same form:
You can drop this into the FormLoad event: