CindyH
CindyH

Reputation: 3026

How to navigate between fields with Enter Key

My company has a large application written in VB6, and for historical reasons, the application is navigated with the Enter key instead of with the Tab key. I don't know VB6, but I know that they currently set the focus for each control in a big select statement in the Form's KeyUp event if it's an EnterKey. Now we are starting to convert to .NET, and have to keep things consistent so the users won't have to TAB on some forms and ENTER on others. I want to write ancestor forms that will automatically ENTER from field to field instead of tabbing. A coworker told me that the way it's done in VB6 is to process buttons not on the CLICK event but on the KEYUP event. I need to continue doing this so I won't have leftover KeyUp events to pass back to VB6 after my form is finished. The order of events for buttons is

  1. button_PreviewKeyDown
  2. button_Click (apparently replacing the KeyPress event)
  3. form_KeyUp
  4. button_KeyUp

I created forms as follows:

Although klugey, this actually seems to work. What I want to know is, is there a better way? Perhaps some setting somewhere that I can tell my application "Enter through forms instead of tabbing"? Are the events that I'm using instead of the click events the best ones?

Upvotes: 2

Views: 1688

Answers (2)

Seph
Seph

Reputation: 8703

A better implementation is to use the Form's AcceptButton property, set it to a hidden button somewhere on the form. Then when the user presses enter the button is clicked, then in the button on-click event you do the code to move to the next control. To select the next control you could just select through the list of items.

The easiest way to do this is to create a base form and then within this have the button and the AcceptButton logic.

Upvotes: 2

Tim Jarvis
Tim Jarvis

Reputation: 18815

Maybe now that you are converting its time to push the idea of adhering to windows standards.

In direct answer to your question, the above mechanism is a pretty common way to do this, another way to do it is to set the key preview property of the form and handle it in the form directly. Neither way is a particulary good solution as Window navigation is simply not meant to be done that way,

Upvotes: 2

Related Questions