Sunday, September 9, 2012

WP7 Application causes the onscreen keyboard to stay on

Setting focus to a TextBox immediately after calling MessageBox.Show() can cause the onscreen keyboard to get stuck on if the user presses the devices start button while the prompt is still displayed.

Once in this state you need to find an application that will display the keyboard so that it can be dismissed normally.

For example, rather than doing something like:

if (string.IsNullOrEmpty(ivm.Name))
{
    MessageBox.Show("Please enter a name");
    // This line will still occur if the user presses the hardware start button while the message box is displayed and will cause the keyboard to appear on the start screen.
    sampleTextBox.Focus();
                
    return;
}

Instead, set the focus before showing the message box so the keyboard can be dismissed by the OS code when the hardware button is pressed.

if (string.IsNullOrEmpty(ivm.Name))
{
    sampleTextBox.Focus();
    MessageBox.Show("Please enter a name");
                
    return;
}