splash27
splash27

Reputation: 2107

How to set the caret to the end of the text in TextBox (WP8)?

I need to write the algorithm that replaces the first character by the same character but in upper case. So, I have written this code:

private void RegionFilter_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox tb = (sender as TextBox);
        var initialText = tb.Text;
        if (initialText != "")
        {
            var firstChar = initialText.Substring(0, 1).ToUpper();
            var restOfString = initialText.Substring(1, initialText.Length - 1);
            tb.Text = firstChar + restOfString;
        }
    }

But there is a problem: The caret doesn't move to the end after replacing the text, it still remains at the beginning.

It's important to say that there is no CaretIndex property in TextBox in Windows Phone 8. How can I solve this?

Upvotes: 1

Views: 364

Answers (2)

Romasz
Romasz

Reputation: 29790

You can also change the position of the caret by setting TextBox.SelectionStart of your TextBox. Add at the end of your algorithm:

yourTextBox.SelectionStart = yourTextBox.Text.Length;

Upvotes: 1

Gaurav Deochakke
Gaurav Deochakke

Reputation: 2283

You can, however, use the selection methods of the textbox like i have shown here:

    myTextBox.Select(tbPositionCursor.Text.Length, 0);

You can find more information about this here:: http://msdn.microsoft.com/en-us/library/ms752349(v=vs.110).aspx

Upvotes: 2

Related Questions