views:

48

answers:

2

I have a simple textbox on my Windows Phone 7 application. I want to execute a method when the user types something in the textbox and confirms it.

My question is, how would I go about this? When I click the textbox in the emulator, a keyboard pops up, I'm guessing the preferred way is somehow capturing that submit event. Any guidance?

+2  A: 

Consider checking for the Enter keypress. Ensure your XAML's TextBox has this method declared for the KeyDown event.

<TextBox Name="textBox1" KeyDown="OnKeyDownHandler"/>

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        Debug.WriteLine("Enter");
        //do the work you want.

        //haven't found yet a good way to hide the keyboard explicitly.
        //setting focus to a control will hide the keyboard. unsure if there's
        //a Keyboard.Hide()
        SomeOtherControl.Focus();
    }
}

Ensure you then hide the keyboard if appropriate. Hiding the keyboard is achieved by setting focus away from the textbox.

p.campbell
Ah so this isn't a 'built-in' thing, I'd have to manually code this. Is this the standard for Windows Phone applications or does the standard say I have to process submission with a button click?
Serg
@Sergio: indeed, it's the same pattern of trapping for the right keystroke, and then doing your work based on the key that was pressed. The other built-in things are the hardware buttons: Win key, Search and Back.
p.campbell
Ok, I've tried out your idea. For example, I want my TextBlock to get focus to hide the keyboard. But there is no such thing as a.Focus() method.
Serg
@Sergio: just looking the application I used previously.... I'd used a `ListBox.Focus()` to trigger the hide of the keyboard.
p.campbell
A: 

Hi Sergio,

Would handling this in LostFocus address your requirement simply?

I just tried enter on a page with two textboxes using the sip and focus moved from one textbox to the other.

Mick N