简体   繁体   中英

C# - I want to hit Enter, not click on the “button” to activate program

I'm just learning C#. I created a Windows Form Application. It works, but you have to click on the "Calculate" button that I created to activate the program. I would like to be able to just hit Enter on my keyboard. I thought maybe there was a properties setting for the button but I don't see one.

Any ideas?

You are looking for Form.AcceptButton property

Gets or sets the button on the form that is clicked when the user presses the ENTER key.

You could listen for the KeyUp event

using System.Windows.Forms;

private void btnSubmit_KeyUp(object sender, KeyEventArgs e)
{
    if(e.KeyCode == Keys.Enter)
        MessageBox.Show("You hit the Enter key.");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM