What's the Best Way to Catch the Return Key in a PasswordBox? (WPF/XAML)
Asked Answered
R

3

22

What's the best way to catch the return key in a PasswordBox? (WPF/XAML)

I have a TextBox field and a PasswordBox field on my login form (for username and password entry). I also have a login button which invokes the method that performs the login validation process.

I need to make the Return key react the same way in the PasswordBox, so that the user can have the option enter their username and password and simply hit Return to log in.

Does anyone know how this is done in WPF? Any help is appreciated.

Resolute answered 24/7, 2009 at 19:15 Comment(0)
M
62

There is an even easier mechanism to activate the button's code. WPF Button class provides a property called IsDefault. When set to true, if you press return while some objects in the window have focus, the code of the click event of the button will be fired automatically. This mechanism works like a charm with the passwordbox.

Metalworking answered 24/7, 2009 at 20:7 Comment(3)
Just to note, the IsDefault, IsDefaulted, and IsCancel properties are not implemented on the button class in WinRT. Disappointing.Pennyroyal
The problem is that if you set the buttons's IsDefault property to true and then hit return while in any textbox (for example, the username textbox) it still runs...Paravane
@JPProgramer I get what you mean, nevertheless, it is the expected behavior on a form.Metalworking
B
6

You can handle the KeyDown event on the PasswordBox (and TextBox if desired) and then use the following event handler --

private void OnKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key != Key.Return && e.Key != Key.Enter)
        return;
    e.Handled = true;
    HandleEnter();
}
Bobsleigh answered 24/7, 2009 at 19:29 Comment(1)
This will work perfectly, I just have trouble finding the events usable for different WPF controls ... Thanks!Resolute
T
1

You could try using a RoutedCommand.

You can set it on the Command property of the Button.
You can also add a KeyGesture to the InputBindings of your loginform to bind the Enter key to trigger your RoutedCommand.

Then add a CommandBinding in the CommandBindings of your loginform to bind the RoutedCommand to Executed handlers in your code and perform or trigger your login validation process.

Tibia answered 24/7, 2009 at 19:20 Comment(2)
Sounds powerful! But is that really the simplest way to do this, now? If so - I'll have an earful for Microsoft, lemme tell ya ...Resolute
Depends. This way you have only one handler in your code instead of two (on for the button, one for the passwordbox). It's more flexible, and not as complex as it sounds. There are only two extra steps which you have to do only once, ever: defining the RoutedCommand and adding the CommandBinding. The KeyGesture is simiilar to adding an eventhandler to the passwordbox, for the button you set a Command instead of the Click handler. But you have a point, it's probably overkill for a simple login form. :)Tibia

© 2022 - 2024 — McMap. All rights reserved.