views:

21

answers:

2

i have a login form

alt text

what i want is that after i have filled the password field when i press "Enter", it performs some action, some set of lines of code, in my case the same that the "Login button does". how to do it

+1  A: 

Add an eventhandler for keypress on the password textbox, in that eventhandler you check for the enter key, and call the (eventhandler) method that the login button is bound to.

klausbyskov
how to check that key is "Enter", pleae elaborate, am new to C#
Junaid Saeed
the KeyPressEventArgs contains a property called KeyCode that you can compare to Keys.Enter.
klausbyskov
+1  A: 

Set the form's AcceptButton to the "Log In" button and write the "lines of code" that perform the login in its click handler.

You can disable the "Log In" button if the username or password is not filled in. Wire up the TextChanged event of both textbox object to an event handler that does this:

 void UserNameOrPasswordTextChanged(object sender, EventArgs e) {
     loginButton.Enabled = !string.IsNullOrEmpty(userNameTextBox.Text) &&
                           !string.IsNullOrEmpty(passwordTextBox.Text);
 }

Handling the Enter key press only in the password field can be very nonintuitive to the user. Assume he fills the password box and moves back to the username field to correct a typo and then presses Enter. It won't work if you have just handled KeyPress for password textbox and checked for it and this will make the user frustrated.

Mehrdad Afshari