I
I
Ivan2020-03-30 23:47:08
WPF
Ivan, 2020-03-30 23:47:08

When adding words to a TextBox via code, the caret is not moved to the end of the phrase. Where is the mistake?

On my form, there are 2 TextBox fields in which I want to make the date input as follows: the user enters the first digits, then a dot is put (through code, not by the user), enters two numbers again, dot again.

public void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (((TextBox)sender).Text.Length == 2 || ((TextBox)sender).Text.Length == 5)
            {
                ((TextBox)sender).Text += ".";
            }
        }


With the above solution, a dot is put, but the caret is moved back a character, how can I fix this?5e825ab4c0205881559981.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-stream, 2020-03-31
@Vamba45

Why not a classic with datepicker https://docs.microsoft.com/ru-ru/dotnet/api/system...
well, or in extreme cases textbox mask https://docs.microsoft.com/en-us/ windows/community...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question