469,930 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,930 developers. It's quick & easy.

Trying to make text box 1 active after text box 2 has enter key pressed

If I have text box 1 (Password Enter) and I have text box 2 (Password Reneter).

After text has been entered in text box 1 and the enter key is pressed to make text box 2 active (the curser will jump from text box 1 to text box 2.

Any ideas
Mar 28 '09 #1
3 5369
3,525 Expert 2GB
After text has been entered in text box 1 and the enter key is pressed to make text box 2 active (the curser will jump from text box 1 to text box 2.
Are you trying to ask "How do I make the cursor move from textbox1 to textbox2 when the user presses the enter key in textbox1?"

In textbox1 handle the KeyPress event something like this
Expand|Select|Wrap|Line Numbers
  1.         private void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
  2.         {
  3.             if ((Keys)e.KeyChar == Keys.Enter) btnLogInRead_Click(this, EventArgs.Empty);
  4.             if ((Keys)e.KeyChar == Keys.Up) tbName.Focus();
  5.             if ((Keys)e.KeyChar == Keys.Right) btnLogIn.Focus();
  6.         }
In this example you can see 3 different actions that could take place:
If [Enter] is pressed it is the same as clicking the "LogInRead" button. This is where you might want to have it shift fields. Me personally, I always expect an enter key to press the default button on the form.
If the [Up] arrow key is pressed, then focus shifts to the "Name" textbox.
if the [right] arrow key is press, focus shifts to the LogIn button (but doesn't click it)
Mar 28 '09 #2
179 100+
Far easier than that is to setup the tab index correctly for all your controls. It should then jump automatically I believe, certainly will if the user presses tab which is consistent with most password UI's.
Mar 30 '09 #3
3,525 Expert 2GB
Its always easier to change the conditions that you want to match the behavior of the program, than to change the behavior of the program to match the user desires. But that's not the way we are supposed to write code. Coders do not tell the client how their work flow is supposed to go, or how they have to work the program to make it behave. We have a responsibility to provide a good user experience in addition to the requested functionality. "Good enough" is never good enough.

In this case BOTH should/could happen. The tab order should be set so standard WIndows conventions of tabing are followed. But the OP's stated desire was to have the same behavior when the [enter] key was pressed.

We love options.

Though I agree with you. Generally speaking I don't use [enter] as a tab. But it can be handy to monitor it just the same. For example you could have a Username and a Password text box with no [submit] button at all. Tab between the two fields and pressing [enter] checks the user's credentials. As you can see in the sample code I provided pressing [enter] is the same as clicking the LogIn button, not the same as tabbing to the next control.

This can let you provide more intuitive behavior than just [tab]. Notice that the up and down keys are mapped to other controls. I think its always nice for a user to not have to hit [tab] 47 times to get to a control directly above the current control. It doesn't take much to have all your text boxes in an array and to calculate that the control directly [up] is the one 16 before the current one, for example. [up] 3 times beats repeatedly mashing [SHIFT] + [TAB]
Mar 30 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Hareth | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.