Click here to Skip to main content
15,890,946 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to make an enter button in c# windows form that work like a key board enter button.

What I have tried:

if (lastFocused != null)
{
SendKeys.Send("{ENTER}");
lastFocused.Text = lastFocused.Text;
}
Posted
Updated 5-Apr-16 1:47am
Comments
CHill60 5-Apr-16 7:18am    
Not sure why you are setting lastFocused.Text equal to itself after sending the key.
What is wrong with what you have?

If you mean "closes the form and returns DialogResult.OK to the calling code", then it's simple.
Drop your button on the form.
Go to the Properties Pane for the button.
Set the DialogResult property to OK
Go to the Properties Pane for the containing form.
Set the AcceptButton property to the button.

Done.
Now, when you press ENTER on your keyboard, or the button itself, the form will close.
 
Share this answer
 
Just go to the property and in that Accept button - choose button1

that's it
 
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