C # Make Enter key behave as if button was pressed

How can I encode it so that when I press the enter key, it behaves as if a button was pressed in an existing form?

Say a button on a form makes a welcome message appear

 private void buttonHello_Click(object sender, EventArgs e)
{
    MessageBox.Show("Hello");
}

How to do this when the enter key is pressed, it does the same (for life, I don’t remember, and it is probably very simple, and I'm really dumb)

+4
source share
5 answers

Winforms? If yes, select FORM. Now change the AcceptButton () property to "buttonHello".

See Form.AcceptButton () :

, , ENTER.

+4

Enter , :

private void Form1_KeyDown(object sender, KeyEventArgs e) 
{
    if (e.KeyCode == Keys.Enter){
        button.PerformClick();
    }
}
+2

- : -

private void tb_KeyDown(object sender, KeyEventArgs e) 
{
    if (e.KeyCode == Keys.Enter)        
        button.PerformClick();
}
0

Key Down . ,

private void form_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode == Keys.Enter)
        buttonHello.PerformClick();
}
0

" ", , butto . , (- ), , . , , @Idle_mind, AcceptButton.

0

All Articles