views:

680

answers:

1

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:

  • On the ANCESTOR form's KeyUp event, checks to see if it's an enter key. If it is an enter key, and the active control is not a button, it moves to the next field in tab order. Otherwise it ignores the key and lets the control handle it. If it is a button, the ancestor doesn't presume to know where the button wants control to go, because it will depend on what the button wants to do when it is "clicked".
  • On the CHILD form's buttons, the click event does nothing, and the processing is duplicated in the KeyUp event and the MouseClick event.
  • The ANCESTOR form has a protected Boolean, EatKeyUp, that can be set to True by the CHILD. This is used when the child form needs to send a MessageBox, because if the user enters through the OK button on the MessageBox, there is still a leftover KeyUp event that will be consumed by the ancestor form.

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?

+1  A: 

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,

Tim Jarvis
I'm aware that Window navigation isn't meant to be done that way. BOY am I aware. But we can't have one screen out of hundreds navigated with tabs and the rest navigated with enters. It wouldn't be sensible to convert all the others to tabs and then later go back and convert to .NET.
CindyH