gesus
gesus

Reputation: 471

Catching enter on KeyUp event in winforms application

I'm working on a winforms application. I have a simple login screen and when i press enter key i catch it in KeyUp(...) event and if credentials are not valid i show a MessageBox. After that when i press Enter again it's caught by the MessageBox and the LoginForm both and there is a infinite loop that firstly message box disappears and then LoginForm is entered with invalid credentials and MessageBox is shown again. The key event should be active for only the MessageBox than i should need to press Enter again for submitting the form again.

How can i handle this situation?

Upvotes: 0

Views: 286

Answers (2)

Chibueze Opata
Chibueze Opata

Reputation: 10054

I'd suggest you create a Login button to handle the credential verification, and then set the button as the Accept Button of your form. It's built for handling this sort of issues. i.e. Once enter is pressed, it would appear as if the button was clicked.

See Example.

Upvotes: 1

SLaks
SLaks

Reputation: 887453

You should handle the KeyDown event.

While the message box is open, KeyDown will be swallowed by it.
(As opposed to KeyUp, which will fire after it is closed)

Upvotes: 2

Related Questions