C# – Press Enter to move to next control

cwinforms

I have a few TextBox on the WinForm. I would like the focus to move to the next control when Enter key is pressed? Whenever a textbox gains control, it will also select the text, so that any editing will replace the current one.

What is the best way to do this?

Best Answer

Tab as Enter: create a user control which inherits textbox, override the KeyPress method. If the user presses enter you can either call SendKeys.Send("{TAB}") or System.Windows.Forms.Control.SelectNextControl(). Note you can achieve the same using the KeyPress event.

Focus Entire text: Again, via override or events, target the GotFocus event and then call TextBox.Select method.