none
Keyboard focus on textbox RRS feed

  • Question

  •  

    hi

     

    I have a Label which when on clicked collapses and a textbox comes in its place.

    I need the textbox to automatically have keyboard focus and the cursor blinking on the textbox when it appears. 

    Any solutions?

     

    thanks

    Wednesday, December 12, 2007 9:34 AM

Answers

  • You can use the following helper method to move the keyboard focus back to TextBox:

    public static class FocusHelper

    {

        public static void Focus(UIElement element)

        {

            if (!element.Focus())

            {

                element.Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(delegate()

                    {

                        element.Focus();

                    }));

            }

        }

    }

    Hope this helps
    Friday, December 14, 2007 6:31 AM
  • Use Dispatcher.BeginInvoke to call a delegate which calls textbox.focus()..... you could use dispatcher priority as applicationidle...

     

    HTH

    Wednesday, December 12, 2007 3:32 PM
    Moderator

All replies

  • Try with textBox.Focus() after you set its Visibility to Visible.
    Wednesday, December 12, 2007 9:44 AM
  •  

    i had tried it out but then the cursor is on the textbox... it is not blinking and im not able to type anything in it.
    Wednesday, December 12, 2007 9:57 AM
  • It works fine on my side. Here is the sample code:

    XAML

    <Window

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        x:Class="UntitledProject1.Window1"

        x:Name="Window"

        Title="Window1"

        Width="640" Height="480">

     

        <StackPanel x:Name="LayoutRoot">

            <Label MouseLeftButtonDown="Label_MouseLeftButtonDown" Width="141" Height="29" Content="Click Me" x:Name="label"/>

            <TextBox x:Name="textBox" Visibility="Collapsed" Width="220" Height="89"/>

        </StackPanel>

    </Window>


    Code Behind

    public partial class Window1

    {

        public Window1()

        {

            this.InitializeComponent();

        }

     

        private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        {

            label.Visibility = Visibility.Collapsed;

            textBox.Visibility = Visibility.Visible;

            textBox.Focus();

        }

    }

    Wednesday, December 12, 2007 10:26 AM
  • You can force the selection like so:

     

    TextBox.SelectionStart = int 0;

    TextBox.SelectionLength = int 0;

     

     

    If this is not working try setting TextBox.SelectedText with some string and see the local values of the TextBox because the other (SelectionStart = 0 and SelectionLength = 0) values are default.

    Wednesday, December 12, 2007 10:38 AM
  • The Textblock is getting focus but it is losing focus as soon as it gets it..

     

     

    Wednesday, December 12, 2007 3:21 PM
  • Use Dispatcher.BeginInvoke to call a delegate which calls textbox.focus()..... you could use dispatcher priority as applicationidle...

     

    HTH

    Wednesday, December 12, 2007 3:32 PM
    Moderator
  • I think this will do what is asked.

     

    <Style TargetType="TextBox" x:Key="asd">
    <Setter Property="VerticalAlignment" Value="Top"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="TextAlignment" Value="Center"/>
    <Setter Property="Width" Value="100"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Text" Value="Click Me!"/>
    <Setter Property="Cursor" Value="Arrow"/>
    <Setter Property="Background" Value="LightGray"/>
    <Style.Triggers>
    <Trigger Property="IsFocused" Value="True">
    <Setter Property="Background" Value="White"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Cursor" Value="IBeam"/>
    <Setter Property="Text" Value=""/>
    <Setter Property="TextAlignment" Value="Left"/>
    </Trigger>
    </Style.Triggers>
    </Style>

     

    Unless what is pretended is not what is asked.
    Wednesday, December 12, 2007 5:14 PM
  • You can use the following helper method to move the keyboard focus back to TextBox:

    public static class FocusHelper

    {

        public static void Focus(UIElement element)

        {

            if (!element.Focus())

            {

                element.Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(delegate()

                    {

                        element.Focus();

                    }));

            }

        }

    }

    Hope this helps
    Friday, December 14, 2007 6:31 AM
  • Brilliant little helper class. A definate addition to any UI developers Bag of tricks !
    Thursday, April 23, 2009 10:40 PM
  • I found the following worked in our application, when the standard approaches failed. I doubt it's recommended, but it's simple:

                System.Windows.Forms.Application.DoEvents();
                Keyboard.Focus(tbxLastName);
    


    C#/ASP.NET/SQL/VB developer
    Friday, October 16, 2009 4:06 PM

  • One solution is to call UpdateLayout () before calling Focus().  For example:

    private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        // Create a Textbox and add it to your window
        TextBox textBox = new TextBox();
        ContentControl myContentControl = new ContentControl();
        myContentControl.Content = textBox;
        MyLayoutRoot.Children.Add(myContentControl);
    
    
        // Give Textbox keyboard focus
        textBox.UpdateLayout();
        textBox.Focus();
    }
    Hope that helps.

      
    Saturday, March 13, 2010 11:31 PM
  • This is the only thing that worked for me: http://apocryph.org/2006/09/10/wtf_is_wrong_with_wpf_focus/

     

    Incase post disappears:

    private delegate void MethodInvoker();
    public static void Focus(UIElement element)
    {
    	//Focus in a callback to run on another thread, ensuring the main UI thread is initialized by the time focus is set
    	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);
    }

     

    Thursday, November 4, 2010 5:20 AM