Search code examples
c#windows-phone-7

Enter key presses button


How do I get the enter key to press a button (when pressed in a text box)?

This is my code:

     private void bar_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            bargo.Click;
        }
    }

'bar' is the name of the textbox.

'bargo' is the name of the button.


Solution

  • You should add the error you're getting in your question, but it looks like it's an issue with the Click call rather than the Enter button. Try this

    private void bar_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            ButtonHandler_Click(bargo,null);
        }
    }
    

    where ButtonHandler_Click is your button's Click event handler.

    Better yet would be to call a method that ButtonHandler_Click also calls, rather than doing all the logic in the ButtonHandler_Click event handler.