Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do you get to the code behing when pressing the enter key on the keyboard eg.when you enter password in the text box then press enter the code for the login button should be executed
Posted

Well let's say that if will be nicer to have a button in the same form, and then in the form properties set the AcceptButton property to a button that will be activated by pressing enter and the code in the click event will be executed.

Actually it is more difficult to explain it than do it. Take a look here for an example
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.acceptbutton.aspx[^]

Cheers!
 
Share this answer
 
v2
Add code to handle the KeyPressed event for your text box. In that handler, add code something like:

C#
if (e.KeyChar == '\r')
{
    // add your code here

    // set the Handled flag to true
    e.Handled = true;
}


Edit: Code Formatted.
 
Share this answer
 
v2
if you want to link button with Enter key press then set that Accept Button property of button control ...
:)
 
Share this answer
 
Just go to the properly window of the form and you will see a property called acceptbutton and if you click on the next colum you will be able to select the login button and this will do the job
 
Share this answer
 
you have to set  "defaultbutton" in form tag  Eg.

C#
<form id="form1" runat="server" defaultbutton="ButtonID">
</form>
 
Share this answer
 

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