Programmatically Closing the Onscreen Virtual Keyboard in Windows Phone Apps

If you have a TextBox control in your Windows Phone app, and you want to allow the user to enter text and then close the on-screen keyboard when the “enter key” is tapped you can do the following.

Simply set the focus to another (non TextBox) control such as the page itself.

Assuming the following XAML:

<TextBox Name="SplitterName" KeyUp="SplitterName_KeyUp"></TextBox>

In the event handler for SplitterName_KeyUp:

private void SplitterName_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        this.Focus();
    }
}

This code assumes the event handler is in the code-behind of the PhoneApplicationPage.  The this.Focus(); statement sets the focus to the page itself, which causes the on-screen keyboard to close.

SHARE:

Pingbacks and trackbacks (3)+

Add comment

Loading