Windows Forms – Simulate Tab with Enter Key
A colleague was trying to figure out the best way to tab between various fields on a data entry form he made using .Net WinForms. He wanted the ability to tab between fields whenever the Enter key is pressed. So basically, he wanted to Tab when the Enter key is pressed. This is what I came up with:
I asked him to add event handlers for each field for which he wanted this ability for the KeyUp event. And then add the following code to the event handler:
if (e.KeyData == Keys.Enter)
{
this.ProcessTabKey(true);
}
What this is doing is that it is calling upon a method at the form level which simulates a Tab Key (the bool parameter tells it whether to go forward or backward). This little tip is the cleanest way I know of doing this. Anyone know better? Please tell.
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

September 26th, 2007 at 7:18 pm
Good one which I was looking for. Thanks.
Venkatesh
November 5th, 2008 at 2:11 am
I like this way better. The test will work whether you use KeyCode or KeyData, but using sendkeys will prevent a loop if you happen to be throwing a messagebox on exit of the control.
If e.KeyCode = Keys.Enter Then
SendKeys.Send(”{TAB}”)
End If