.NET TextBox - Handling the Enter Key

Add a keypress event and trap the enter key

Programmatically it looks kinda like this:

//add the handler to the textbox
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckEnterKeyPress);

Then Add a handler in code...

private void CheckEnterKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
        if (e.KeyChar == (char)Keys.Return)

        {
           // Then Do your Thang
        }
}

Inorder to link the function with the key press event of the textbox add the following code in the designer.cs of the form:

 this.textbox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDownHandler);

Now define the function 'OnKeyDownHandler' in the cs file of the same form:

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{

    if (e.KeyCode == Keys.Enter)
    {
       //enter key has been pressed
       // add your code
    }

}