John
John

Reputation: 681

Search box and hitting enter key to execute a function

I have a searchbox where the user of my app can type in a word. I would like to be able to click the "Enter" key on the phones keyboard to start the search, but that doesn't work so instead I need to have a button to launch the search function.

<TextBox Name="txtSearchBox" InputScope="Search"/>

How can I make the enter key launch a function?

Thanks in advance.

Upvotes: 0

Views: 2585

Answers (2)

Mariioo
Mariioo

Reputation: 1

The suggested answer doesn't work anymore in C#

so here is my code that does work

    private void searchbox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            searchButton_Click(this, new EventArgs());
        }
    }

Upvotes: 0

0xFF
0xFF

Reputation: 4178

You can catch the key pressed in the Key_Up event and compare it to the enter key

private void txtSearchBox_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter) 
             DoSearchHere();
    }

Upvotes: 3

Related Questions