Search code examples
c#windows-runtimewindows-phone-8.1windows-8.1richeditbox

How can I move the cursor to the end of the text in RichEditBox?


I'm building a Windows Phone 8.1/Windows 8.1 app (WinRT) and I'm using a RichEditBox control. Every time I add text to it, the cursor goes to the beginning of the text and I can't find a way to move it to the end of the text.

I've build two methods to Set and Add text:

public static void SetText(this RichEditBox e, string text)
{
    e.Document.SetText(Windows.UI.Text.TextSetOptions.None, text); 
}

public static string GetText(this RichEditBox e)
{
   string value;
   e.Document.GetText(Windows.UI.Text.TextGetOptions.AdjustCrlf, out value); 
   return  value;
 }

And I'm using this code to add text to it:

StatusBox.SetText(StatusBox.GetText() +
                                  texttoadd);

Now, how can I move the cursor to the end of the text?


Solution

  • By manipulating the Selection property in the Document property of the RichEditBox

    var newPos = StatusBox.GetText().length-1;
    StatusBox.Document.Selection.SetRange(newPos,newPos)
    StatusBox.Focus(FocusState.Keyboard);