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.

One Response to “Windows Forms – Simulate Tab with Enter Key”

  1. Good one which I was looking for. Thanks.

    Venkatesh

Leave a Reply