Windows Store App – Focus on TextBox when Navigated To The Page

Some time you want to make your app more user friendly by automatically focus on specify control for example search text box so that when user open the page, they can start typing the keyword they want to search rather than click on the search text box first then only start typing.

I try to use TextBox.Focus() in OnNavigatedTo, but it nothing happen. So I try to use the same method use in Silverlight by creating a DispatcherTimer and delay the focus by 0.5 second. Well, this method work out in Windows Store App. If you are interested, below are the code.

// timer to create initial focus on control
DispatcherTimer initialFocusDispatcherTimer = new DispatcherTimer();
initialFocusDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
initialFocusDispatcherTimer.Tick += (tickSender, tickEvent) =>
    {
        initialFocusDispatcherTimer.Stop();

    // focus on search bar
    SearchTextBox.Focus(Windows.UI.Xaml.FocusState.Keyboard);
    };
initialFocusDispatcherTimer.Start();

 

 

by Ooi Keng Siang via Ooiks’s Blog

One thought on “Windows Store App – Focus on TextBox when Navigated To The Page

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s