Click here to Skip to main content
15,885,941 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hey,

I have a windows form with C# language and am using vs2010.

I have a username box, password box and button for enter (login).

Now the problem is, if I type the right password in, then I want that if I hit the enter key on my keyboard it automatically hits the enter button btw. automatically logs in without to click on the enter button.

How do I do that?
Posted
Comments
Sergey Alexandrovich Kryukov 1-Oct-14 16:03pm    
What have you tried so far?
—SA
this.CodeProject.Enabled = true; 1-Oct-14 16:07pm    
I have searched whole google to find the answer of my own, couldn't find anything btw. what gets my problem a answer, and you give me this. (deleted some stupid things I said)
Sergey Alexandrovich Kryukov 1-Oct-14 16:14pm    
We don't accept this kind of talking here.

And it looks so weird, to talk this way to the one who actually provided you with a comprehensively detailed answer, spent time on that, and did something you could easily do by yourself.

Sorry.
—SA

1 solution

Handle the event KeyDown (or even KeyUp) of the TextBox with a password:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keyup%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs%28v=vs.110%29.aspx[^].

In the event handler code, respond to the key System.Windows.Forms.Enter:
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keycode%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keys%28v=vs.110%29.aspx[^].

When it is Enter, call the same thing you call on the Click event handler on your "OK" button (or end of modal state of the dialog on OK, if you have any). It would be also good to check up the values of your user name and password field and disable OK and this keyboard action unless they both have some data, non-empty and not containing only blank spaces.

—SA
 
Share this answer
 
Comments
this.CodeProject.Enabled = true; 1-Oct-14 16:08pm    
But thanks for this, no code again only sites but ok.
Sergey Alexandrovich Kryukov 1-Oct-14 16:13pm    
You are welcome. Will you accept the answer formally (green "Accept" button)?

You don't need code to solve this simple problem; MSDN documentation has everything to do it; and I gave you exact recipe.

—SA
this.CodeProject.Enabled = true; 1-Oct-14 16:24pm    
I know, sometimes if I get answers like this then I get a bit strange, sorry for that. But thanks. I tried right now with the KeyDown, but if I hit enter on my keyboard in the passwordbox then it popsup a few times that im logged in btw. if I hit enter the first time it happen nothing but then again then it pops up.
Sergey Alexandrovich Kryukov 1-Oct-14 16:35pm    
Sure. No problem.

(Just a warning: you already got abuse reports on your account, from two different members, which can lead to cancellation of your account if it continues. Most people don't like bad words in posts, as well as wasting time. Nothing personal — I'm here to help, not to scandal.)

—SA
this.CodeProject.Enabled = true; 1-Oct-14 16:56pm    
It's fine, I have a lot of emails to register a new account, and I use proxys. But I understand, I know sometimes I react different, I will take care of it next time and write a better question btw. search more on google then.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900