Focus on EditorSeachView

SyntaxEditor for WPF Forum

Posted 8 years ago by John Dunn
Version: 11.1.0544
Avatar

There were a few old questions about this but nothing looked like a definitive answer. I have a EditorSearchView in a DockPanel. When a command is executed I show/hide the search view. When it is shown I'd like to throw focus to the 'Find what' text box. Calling .Focus() doesn't seem to work. Here's an example program.

 

using System.Windows;
using System.Windows.Input;

namespace ActiproTest
{
  public partial class MainWindow : Window
  {
    public static RoutedUICommand FindCommand = new RoutedUICommand(
      "Find",
      "Find",
      typeof(MainWindow),
      new InputGestureCollection(new KeyGesture[] { new KeyGesture(Key.F, ModifierKeys.Control, "F") }));

    public MainWindow()
    {
      InitializeComponent();
    }
    private void OnFind(object sender, RoutedEventArgs e)
    {
      if (search.IsVisible)
      {
        search.Visibility = System.Windows.Visibility.Collapsed;
      }
      else
      {
        search.Visibility = System.Windows.Visibility.Visible;
        search.Focus();
      }
      e.Handled = true;
    }
  }
}

 

<Window 
  x:Class="ActiproTest.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:acti="http://schemas.actiprosoftware.com/winfx/xaml/syntaxeditor"
  xmlns:local="clr-namespace:ActiproTest"
  Title="MainWindow" Height="350" Width="525">
  <Window.CommandBindings>
    <CommandBinding Command="local:MainWindow.FindCommand" Executed="OnFind"/>
  </Window.CommandBindings>
  <DockPanel LastChildFill="True">
    <Button 
      Command="local:MainWindow.FindCommand"
      Width="16" 
      DockPanel.Dock="Right"></Button>
    <acti:EditorSearchView 
      x:Name="search"
      DockPanel.Dock="Right"/>
    <acti:SyntaxEditor/>
  </DockPanel>
</Window>

Comments (4)

Answer - Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi John,

More recent versions have an EditorSearchView.FocusFindWhatTextBox method you can call.


Actipro Software Support

Posted 6 years ago by John Dunn
Avatar

Sorry to ressurect an old thread but if I call FocusFindWhatTextBox in my RoutedUICommand handler the focus doesn't work correctly the first time. 

 

    private void OnFind(object sender, RoutedEventArgs e)
    {
      if (search.IsVisible)
      {
        search.Visibility = System.Windows.Visibility.Collapsed;
      }
      else
      {
        search.Visibility = System.Windows.Visibility.Visible;
        search.FocusFindWhatTextBox();
      }
      e.Handled = true;
    }
Answer - Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi John,

My guess is that since you are setting it visible, the WPF layout routines haven't run yet and thus the visuals haven't been realized.  You might try calling UpdateLayout() before the focus call, and if that doesn't work, you'd probably have to run the focus call in a Dispatcher.BeginInvoke wrapper and set the dispatcher priority of that call to be after Layout.


Actipro Software Support

Posted 6 years ago by John Dunn
Avatar

Thanks, calling UpdateLayout fixed the issue.

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.