Johnny Sayasane Johnny Sayasane - 5 months ago 33
C# Question

Try to get Textbox.SelectionStart value before losing focus

A simple question but I still cannot find any solution for this situation.
I want to get the caret position before the text triggers LostFocus event, because if I get the value in the LostFocus event method, it already reset to 0.
Help me find out this problem please.

Thank you in advance,
Johnny Sayasane.


The TextBox class supports an event called "SelectionChanged" , you could try to use this event to set an integer to get the caret's position, check this code:

    private int CaretPosition;

    private void textBox_SelectionChanged(object sender, RoutedEventArgs e)
        CaretPosition = textBox.SelectionStart;