tags:

views:

28

answers:

1

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 returns as 0 when i call focusedTextBox.CaretIndex. At that point I need to 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?

A: 

This just doesn't work in c# express edition 2008 (whatever version of WPF that is) so the best thing to do here is to switch to onTextChanged and run the exact same code there, which works a treat. Its obviously annoying that you have to run the code multiple more times than if it worked on lost focus. When I get the time I will check if it works in c# express 2010 as we are upgrading (eventually)

DrLazer