This was very simple in VB.NET, I would just do the following
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
e.Handled = True
cmdOk.PerformClick()
End If
End Sub
I can’t figure out how to do the equivalent in C#, not for the text box or the form in general. In Form1.cs, in the top left where the drop-down menu thing is, there is no option to generate a method for events (like how you can generate a Sub in VB), all I have is WindowsFormsApplication1.Form1
Select the textbox first. Ensure you are seeing the Properties window, use the View menu if you don’t. Click the lightning bolt icon and locate the KeyPress event. Double click it. Then make it look similar to this:
This is not in fact the Right Way to do it, not in VB.NET either. Set the form’s AcceptButton property to cmdOk so you don’t have to write any code. You’ll now also get a heavy border around the OK button so the user knows that this is the key that gets activated when she presses Enter. Pick up a book about Winforms programming from your local library, these things are hard to guess at through trial and error.