Get the caret position when losing focus in WPF

Posted by DrLazer on Stack Overflow See other posts from Stack Overflow or by DrLazer
Published on 2010-06-01T11:58:13Z Indexed on 2010/06/01 12:03 UTC
Read the original article Hit count: 329

Filed under:
|
|

I have two textbox's. I have an event setup for the "onLostFocus" of the textbox's. If i finish typing a word into both boxes one after the other all is well. At the point where i click back on the first textbox i want to click halfway through the word (perfectly resonable thing for a user to do). When the onLostFocus event fires here is my code :

void tbox_LostFocus(object sender, RoutedEventArgs e)
      {
         IInputElement focusedelement = FocusManager.GetFocusedElement(this);
         if (focusedelement is TextBox)
         {
            TextBox focusedTextBox = focusedelement as TextBox;
            int focusIndex = m_commandsStacker.Children.IndexOf(focusedTextBox);
            int caretIndex = focusedTextBox.CaretIndex;

The caret index is 0. At that point I refresh the whole form and set parameters and all other kinds of whizzery, storing the texbox index and caret position to put everything back the same. Why does it return 0 and not the position where the caret should be halfway between the word?

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf