Click here to Skip to main content
15,895,808 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am new at learning c#, can anyone tell me how I can do this ?
Posted
Updated 12-May-11 12:09pm
v2
Comments
Dalek Dave 12-May-11 18:09pm    
Edited for Grammar and Syntax.

All you need to do is to check if event.keyCode == 13 and perform the .click() operation on the button.


Take a look at the following link..

Stack Overflow Link[^]
BR//
Harsha
 
Share this answer
 
v2
Comments
Dalek Dave 12-May-11 18:10pm    
Good call and good link
I'm assuming you want to "click" a button when the user presses enter on a textbox, for example, and that you're working with winforms.

You need to handle the KeyDown or KeyUp event of the textbox and depending on the key pressed call the PerformClick() event of the button.
 
Share this answer
 
Comments
Dalek Dave 12-May-11 18:10pm    
Seems reasonable.
Do your want to work on Button_Click() event on clicking the button or something else? Please explain.
 
Share this answer
 
Comments
kastriotlimani 12-May-11 14:12pm    
i am looking to create a database .. and i have some textboxes to execute queries and i also have one button search. All i want is to press enter instead of pressing that search button but i dont know how ... sory for my weak english
Please note that Button Control also works with enter key (if focused when enter key pressed). You can use tab to move your focus to that button. And Tabindex property of form controls is useful to set tab index for your control.
 
Share this answer
 
You can set AcceptButton[^] property to the button in your Form what handles the data entered.

And remember, just setting the AcceptButton/CancelButton is not enough. This just tell which button should be invoked on ENTER/ESC. We have to set the DialogResult in the Button handler.

C#
button1.DialogResult = System.Windows.Forms.DialogResult.OK;
button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;

form1.AcceptButton = button1;
form1.CancelButton = button2;


And... (Kim Togo...Don't Repeat Yourself) - Look at this answer[^]:
 
Share this answer
 
v2

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