How to set focus in a programmatically created text box?

What do I need to do for the following code so that the cursor blinks in the second text box when the window appears?

XAML:

<Window x:Class="TestFocksdfj.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel HorizontalAlignment="Left" Margin="10">
        <ContentControl x:Name="FormArea"/>
    </StackPanel>
</Window>

Code for:

using System.Windows;
using System.Windows.Controls;

namespace TestFocksdfj
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            StackPanel sp = new StackPanel();

            for (int i = 0; i < 3; i++)
            {
                TextBox tb = new TextBox();
                tb.Width = 200;
                tb.Margin = new Thickness { Bottom = 3 };
                if (i == 1)
                    tb.Focus();
                sp.Children.Add(tb);
            }

            FormArea.Content = sp;
        }
    }
}
+3
source share
3 answers

After you called FormArea.Content = sp;, you can call sp.Children[1].Focus();to highlight the focus of the second text field.

Like this:

public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            StackPanel sp = new StackPanel();

            for (int i = 0; i < 3; i++)
            {

                TextBox tb = new TextBox();
                tb.Width = 200;
                tb.Margin = new Thickness { Bottom = 3 };
                sp.Children.Add(tb);
            }
            FormArea.Content = sp;
            sp.Children[1].Focus();
        }
    }
+4
source

http://apocryph.org/2006/09/10/wtf_is_wrong_with_wpf_focus, , WPF ?

using System.Windows;
using System.Windows.Controls;
using System.Threading;
using System;
using System.Windows.Input;

namespace TestFocksdfj
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            StackPanel sp = new StackPanel();

            for (int i = 0; i < 3; i++)
            {
                TextBox tb = new TextBox();
                tb.Width = 200;
                tb.Margin = new Thickness { Bottom = 3 };
                if (i == 2)
                {
                    FocusHelper.Focus(tb);
                }
                sp.Children.Add(tb);
            }

            FormArea.Content = sp;
        }
    }

    //thanks to: http://apocryph.org/2006/09/10/wtf_is_wrong_with_wpf_focus/
    static class FocusHelper
    {
        private delegate void MethodInvoker();

        public static void Focus(UIElement element)
        {
            ThreadPool.QueueUserWorkItem(delegate(Object foo)
            {
                UIElement elem = (UIElement)foo;
                elem.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    (MethodInvoker)delegate()
                    {
                        elem.Focus();
                        Keyboard.Focus(elem);
                    });
            }, element);
        }
    }

}
0

You might think tb.focus would be all that is required. You can try setting tabindex for the second text field to 0, and then try tb.focus. Another alternative is this bit of JavaScript ...

private void Set_Focus(string controlname)
{
string strScript;

strScript = "<script language=javascript> document.all('" + controlname + "').focus() </script>";
RegisterStartupScript("focus", strScript);
}
0
source

Source: https://habr.com/ru/post/1732565/


All Articles