Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9053703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:29:36+00:00 2026-06-16T13:29:36+00:00

This is a weird one and I don’t really even know what to search

  • 0

This is a weird one and I don’t really even know what to search for, but trust me I have.

I have a text box and bound to its OnTextChanged event is the below method.

The purpose here is to give the text box focus, move the cursor to the end of the TextBox and return focus back to whatever was actually focused (usually a button). The problem is that it seems the TextBox is not “redrawn” (for lack of a better word?) before I send the focus back to the originally focused element so the cursor position does not update on screen (though all properties think it has).

Currently, I have brutally hacked this together that basically delays the refocus of the previous focused item by 10 ms and runs it in a different thread so the UI has time to update. Now, this is obviously an arbitrary amount of time and works fine on my machine but someone running this app on an older machine may have problems.

Is there a proper way to do this? I can’t figure it out.

private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
    if (sender == null) return;
    var box = sender as TextBox;

    if (!box.IsFocused)
    {

        var oldFocus = FocusManager.GetFocusedElement(FocusManager.GetFocusScope(this));
        box.Select(box.Text.Length, 0);
        Keyboard.Focus(box); // or box.Focus(); both have the same results

        var thread = new Thread(new ThreadStart(delegate
                                                    {
                                                        Thread.Sleep(10);
                                                        Dispatcher.Invoke(new Action(() => oldFocus.Focus()));
                                                    }));
        thread.Start();
    }
}

EDIT

A new idea I had was to run the oldFocus.Focus() method once the UI is done updating so I tried the following but I get the same result 🙁

var oldFocus = FocusManager.GetFocusedElement(FocusManager.GetFocusScope(this));

Dispatcher.Invoke(DispatcherPriority.Send, new Action(delegate
 {
   box.Select(box.Text.Length, 0);
   box.Focus();
 }));

Dispatcher.Invoke(DispatcherPriority.SystemIdle, new Action(() => oldFocus.Focus()));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:29:37+00:00Added an answer on June 16, 2026 at 1:29 pm

    After many days, I was finally able to get it to work. It required the Dispatcher to check if the textbox has both focus AND keyboardfocus and lots of loops.

    Here’s the code for reference. There’s some comments in it but if anyone hits this page looking for an answer, you’ll have to read through it yourself. A reminder, this is on text change.

    protected void TextBox_ShowEndOfLine(object sender, TextChangedEventArgs e)
        {
            if (sender == null) return;
            var box = sender as TextBox;
    
            if (!box.IsFocused && box.IsVisible)
            {
                IInputElement oldFocus = FocusManager.GetFocusedElement(FocusManager.GetFocusScope(this));
                box.Focus();
                box.Select(box.Text.Length, 0);
                box.Focus();
    
                // We wait for keyboard focus and regular focus before returning focus to the button
                var thread = new Thread((ThreadStart)delegate
                                            {
                                                // wait till focused
                                                while (true)
                                                {
                                                    var focused = (bool)Dispatcher.Invoke(new Func<bool>(() => box.IsKeyboardFocusWithin && box.IsFocused && box.IsInputMethodEnabled), DispatcherPriority.Send);
                                                    if (!focused)
                                                        Thread.Sleep(1);
                                                    else
                                                        break;
                                                }
    
                                                // Focus the old element
                                                Dispatcher.Invoke(new Action(() => oldFocus.Focus()), DispatcherPriority.SystemIdle);
                                            });
                thread.Start();
            }
            else if (!box.IsVisible)
            {
                // If the textbox is not visible, the cursor will not be moved to the end. Wait till it's visible.
                var thread = new Thread((ThreadStart)delegate
                                            {
                                                while (true)
                                                {
                                                    Thread.Sleep(10);
                                                    if (box.IsVisible)
                                                    {
                                                        Dispatcher.Invoke(new Action(delegate
                                                                                         {
                                                                                             box.Focus();
                                                                                             box.Select(box.Text.Length, 0);
                                                                                             box.Focus();
    
                                                                                         }), DispatcherPriority.ApplicationIdle);
                                                        return;
                                                    }
                                                }
                                            });
                thread.Start();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one is really weird. I have multiple $.post() in the code, but there
Ok, this is a really weird one. I have an MPI program, where each
I know this one is the weirdest of all weird questions I have asked
I dunno guys, this is a really weird one, but I might just be
I don't know if this is default behavior or not, but it seems weird
this is a weird one. i have a -pre-compile target in my custom_rules.xml .
This is a weird one, but hopefully someone can give me an idea here.
Ok, this one is weird, i have this code: $('#nps').submit(function(e) { e.preventDefault(); var images
Boy, this one is really weird. I expect the following code to print 1990,
Ok, this one is really weird... I can't show code for it exactly cause

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.