14

How do I code it so that when the enter key has been pressed it behaves as if a button on the existing form has been pressed?

Let's say the button on the form makes it so a display message of hello shows up

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

How do I make it so when the enter key is pressed it does the same thing (for the life of me I can't remember and it's probably really simple and I'm being really dumb)

user2867035
  • 165
  • 1
  • 1
  • 6
  • You should look at the [`KeyUp`](http://msdn.microsoft.com/en-us/library/system.windows.uielement.keyup.aspx) event – Harrison Oct 24 '13 at 18:14
  • possible duplicate of [How do I tell when the enter key is pressed in a TextBox?](http://stackoverflow.com/questions/11806166/how-do-i-tell-when-the-enter-key-is-pressed-in-a-textbox) – Harrison Oct 24 '13 at 18:16

7 Answers7

26

WinForms? If yes, select the FORM. Now in the Properties Pane (bottom right of the screen by default) change the AcceptButton property to "buttonHello".

See Form.AcceptButton:

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

Here's what it looks like in the Properties Pane:

enter image description here

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
9

Capture the Enter key down event, like this:

private void Form1_KeyDown(object sender, KeyEventArgs e) 
{
    if (e.KeyCode == Keys.Enter){
        button.PerformClick();
    }
}
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
4

In your Form properties, set AcceptButton = yourButton, That's it.

Óûth Mânē
  • 59
  • 1
  • 8
1

add a Key Down event handler to your form. Then check if the enter key has been pressed

private void form_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode == Keys.Enter)
        buttonHello.PerformClick();
}
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
1

For WinForms:

using System.Windows.Forms; // reference

private void Form1_KeyDown(object sender, KeyEventArgs e) 
{
    if (e.KeyCode == Keys.Enter)
    {
        // do something
    }
}

For WPF:

using System.Windows.Input; // reference

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        // do something
    }
}

Notes

  • This example is for Enter button, you can easy change that changing Keys.YOURKEY.
  • You need to add first the event in your form or window.
Marco Concas
  • 1,665
  • 20
  • 25
0

Are you looking for something like this:-

private void tb_KeyDown(object sender, KeyEventArgs e) 
{
    if (e.KeyCode == Keys.Enter)        
        button.PerformClick();
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

You are driving toward the "Magic Button" antipattern, and the symptom is that you want to drive the butto from somewhere else. Insulate the behavior you want from the button in some way ( a member function could work ) and then call the same function from any point you like. If you really just want to reply to the enter key, best way is, as suggested by @Idle_mind, use the AcceptButton.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115