locked
Setting SelectionStart property of TextBox does not bring up a small circle below the caret.

    Question

  • Hello!

    I develop a Windows Store App for Windows 8.1. I have a TextBox (namespace Windows.UI.Xaml.Controls) where user enters his email address. The task is to set a caret before “@” whenever user taps the TextBox and to allow user to move the caret with his finger if he needs it somewhere else in the text.

    I tried to use Tapped event handler of the TextBox and set there SelectionStart property to position the caret before “@”.

    First thing that I faced was that the code in the event handler was not executed. I found a solution on stackoverflow forums and added the following line to my constructor:

    textBoxEmail.AddHandler(TappedEvent, new TappedEventHandler(TextBoxEmailTapped), true);

    After that a caret was set before “@” when I tapped but there was no circle below the caret. So user can’t move the caret with a finger.

    The code of the event handler is the following:

    private void TextBoxEmailTapped(object sender, TappedRoutedEventArgs e)

            {

                try

                {

                    TextBox textBox = (TextBox)sender;

                    string email = textBox.Text;

                    int position = email.IndexOf("@");

                    if (position >= 0)

                    {

                        textBox.SelectionStart = position;

                    }

                }

                catch (Exception ex)

                {

                    Logger.Current.Error("Error while trying to position caret in the email text box in settings window", ex);

                }

            }

    How can I achieve the needed behavior?

    I would be very grateful for your help. 

    Best regards,

    Margarita.


    • Edited by Rita(Korus) Monday, August 04, 2014 9:02 AM
    Monday, August 04, 2014 8:52 AM

All replies

  • Does it help if you also set selectionlength = 0?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, August 04, 2014 7:55 PM
    Moderator
  • Hello Matt,

    >Does it help if you also set selectionlength = 0?

    Unfortunately it doesn't. The result is the same.

    I also tried to use textBox.Select(position, 0); - result is the same .

    The only possibility to bring up the caret (at least the only possibility I managed to come up with) is tapping again on the text box on the same location where the caret currently is. But it won’t suit the users…

    Margarita.


    Tuesday, August 05, 2014 7:50 AM