简体   繁体   中英

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.

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();
    }

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());
        }
    }

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