locked
Searchbox and Focus

    Question

  • Hi everybody. I'm using a searchbox in my app and the every time I start the app it is focus and the history expanded. Is there any way I can keep the history enabled but the searchbox not focused when I start the app? I already tried to set the focus to different control but with no success. Thank you.
    • Moved by Mike Danes Monday, August 11, 2014 5:47 AM winstore specific
    Sunday, August 10, 2014 1:04 PM

Answers

  • Be very careful using dummy controls as focus grabbers. This is a very good way to break your tab order and confuse keyboard and non-sighted users.

    If you don't have a logical control to set focus to other than the SearchBox then you can set the focus to the page itself when the page is Loaded:

    <Page
        x:Class="App189.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App189"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        IsTabStop="True" 
        Loaded="Page_Loaded"
        >
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            
            <SearchBox 
                SearchHistoryEnabled="True"
                 Height="35" Width="270"
                 Margin="0, 25, 25, 0"  />
        </Grid>
    </Page>
            private void Page_Loaded(object sender, RoutedEventArgs e)
            {
                this.Focus(FocusState.Programmatic);
            }

    Another possibility to avoid having the page in the tab order is not to put the SearchBox on the page initially (or put it in Collapsed). Instead put a button templated with the search icon and swap it for the SearchBox on Click or LostFocus. The hitch here is that handing immediate keyboard input will be tricky. I haven't tried it, but you may be able to include a TextBox, swap on TextUpdated, and copy the entered text into the SearchBox's TextBox.

    • Marked as answer by giannisdolon Wednesday, August 27, 2014 7:20 PM
    Wednesday, August 27, 2014 2:20 AM
    Owner

All replies

  • Depends.

    What sort of app?

    Winforms, webforms, wpf, winphone, windows store app what?

    What is the history you are talking about?

    What do you mean expanded - what sort of control?

    What relationship does this history and expended thing have to the search box?

    What other controls are on the window/form/page/whatever?

    .

    SearchBox sounds winforms or win store app.

    I'll take a guess this is app store or at least close enough the same advice will work.

    You could try initially setting IsTabstop=false.

    Something like:

    <SearchBox IsTabStop="false"/>

    Then some other control will receive focus, assuming there is some other control to do so.

    Once the Page or whatever has completed loading and set everything up you could set that true again.I'm not sure which is the best event in win app store.  Maybe Page GotFocus. There's no equivalent to the wpf contentrendered iirc.

    Sunday, August 10, 2014 2:08 PM
  • Ok, sorry for the lack of information. It's a windows store app History is the SearchHistoryEnabled option that shows previous searches, which is Searchboxe's property. The problem is that I don't have any other textbox in the app that I could focus. Only a viewlist that is not selectable.
    Monday, August 11, 2014 4:35 AM
  • I would suggest to add a focusable non-text control to your xaml, and give it an empty control template so it remains invisible:

    <Slider x:Name="FocusGrabber">
         <Slider.Template>
             <ControlTemplate />
         </Slider.Template> 
    </Slider>

    That should get the focus without visual impact.

    If it doesn't, then you can assign the focus programmatically to it:

    this.FocusGrabber.Focus(Windows.UI.Xaml.FocusState.Programmatic);

    Make sure you use a non-text control. I you would use this one:

    <TextBox x:Name="FocusGrabber">
         <TextBox.Template>
             <ControlTemplate />
         </TextBox.Template> 
    </TextBox>
    it'll also get the focus, but then the soft keyboard pops open, which is probably not what you want.

    Monday, August 11, 2014 6:08 AM
  • Ok, sorry for the lack of information. It's a windows store app History is the SearchHistoryEnabled option that shows previous searches, which is Searchboxe's property. The problem is that I don't have any other textbox in the app that I could focus. Only a viewlist that is not selectable.

    Did you try my suggestion?

    What happened?

    Just set IsTabStop="false" and spin it up.


    Monday, August 11, 2014 9:01 AM
  • Unfortunately, both ideas where good but none worked. Still the searchbox is expanded at the start op the app. 
    Monday, August 11, 2014 4:41 PM
  • Hi giannisdolon,

    Problem solved?

    Take a look at the SearchBox Sample: http://code.msdn.microsoft.com/windowsapps/SearchBox-control-sample-0f64f94d

    You could see the search box does not get focused when the app start and search box keep both search history and search suggestion list.

    If your problem does not solve, could you share your code with us? I agree with Diederik, if there is no other controls can be focused, I often use a width=0, height=0 button as a focus gabber.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, August 26, 2014 3:20 AM
    Moderator
  • Be very careful using dummy controls as focus grabbers. This is a very good way to break your tab order and confuse keyboard and non-sighted users.

    If you don't have a logical control to set focus to other than the SearchBox then you can set the focus to the page itself when the page is Loaded:

    <Page
        x:Class="App189.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App189"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        IsTabStop="True" 
        Loaded="Page_Loaded"
        >
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            
            <SearchBox 
                SearchHistoryEnabled="True"
                 Height="35" Width="270"
                 Margin="0, 25, 25, 0"  />
        </Grid>
    </Page>
            private void Page_Loaded(object sender, RoutedEventArgs e)
            {
                this.Focus(FocusState.Programmatic);
            }

    Another possibility to avoid having the page in the tab order is not to put the SearchBox on the page initially (or put it in Collapsed). Instead put a button templated with the search icon and swap it for the SearchBox on Click or LostFocus. The hitch here is that handing immediate keyboard input will be tricky. I haven't tried it, but you may be able to include a TextBox, swap on TextUpdated, and copy the entered text into the SearchBox's TextBox.

    • Marked as answer by giannisdolon Wednesday, August 27, 2014 7:20 PM
    Wednesday, August 27, 2014 2:20 AM
    Owner
  • Thank you very much. It actually worked by setting the focus to the page. Thanks again.
    Wednesday, August 27, 2014 7:21 PM