Forum Xamarin Xamarin.Forms

How to programmatically insert some text in Entry in any cursor position?

Hi All Xamarin Experts / Developers,

I am facing issue related to programmatically insertion of text in Entry. Could you please provide some suggestion to solve this issue.

The scenario is as:

  • User is tapping (after "xamarin word") in Entry which is having some text i.e "This is xamarin Forms",
  • Now he is tapping on button to insert some text in that Entry where he was tapped (after "xamarin word").

Here, when user will try to tap on button to insert some text in the Entry in some position then he will loose focus from Entry and tapped location will be emitted.

Any suggestion how to achieve it?

Best Answers

  • sahmadsahmad Member ✭✭
    Accepted Answer

    I got solution as:

    Xaml:
    <Entry x:Name="entry />

    .cs
    var position = entry.CursorPosition;
    entry.Text = entry.Text.Insert(position, "entry");

    its working for me. Thanks a lot @JohnHardmanJohnHardman :)

Answers

  • JohnHardmanJohnHardman GBUniversity admin

    @sahmad - Is there some reason the user cannot just use the Paste option that is supported by the Entry view?

    (Long press on the Entry on Android/iOS, or right-click on UWP)

  • sahmadsahmad Member ✭✭

    @JohnHardmanJohnHardman , We know user have option to copy some text and then paste anywhere in the entry but here scenario is different.
    Actually user want to retrieve some text from api call and want to insert that text in the position of cursor of enrty where he was before using the button tap.

  • sahmadsahmad Member ✭✭

    @JohnHardmanJohnHardman Thanks for your reply :)

    Now, I have found this property for entry in the latest forms update and getting cursor position but I want to insert some text in that cursor position.
    Entry is allowing .Text property which is just inserting new text and replacing old one.
    Is there any way to insert new text for the given cursor position in the entry?

  • sahmadsahmad Member ✭✭

    @JohnHardmanJohnHardman...Thanks its a great idea to use substring() for inserting new text.

    Thanks for your immediate responses..It was really helpful :)

  • sahmadsahmad Member ✭✭
    Accepted Answer

    I got solution as:

    Xaml:
    <Entry x:Name="entry />

    .cs
    var position = entry.CursorPosition;
    entry.Text = entry.Text.Insert(position, "entry");

    its working for me. Thanks a lot @JohnHardmanJohnHardman :)

  • sahmadsahmad Member ✭✭
    edited October 2018

    Hi @JohnHardmanJohnHardman ,

    The above solution worked for me for Entry control and I have a need to apply the same functionality for Editor as well where data is displaying in multilines.

    As I have checked Editor don't have the same"CursorPosition" property

    I need to display data in multilines and need to track cursor position for Editor as well,
    but its not supporting:

    <Editor x:Name="editor1" Text="Plain Text" CursorPosition="3"></Editor>
    

    Can you please provide any solution for it?

    Thanks in Advance.

  • JohnHardmanJohnHardman GBUniversity admin

    @sahmad - As you said, Editor does not currently implement a CursorPosition property. Until it does, you would want to subclass Editor and then create custom renderers for your subclass. I haven't done that, but wouldn't expect it to be difficult - you might even be able to adapt the code from the EntryRenderer implementations that can be found in the Xamarin.Forms source code.

    The only complication that springs instantly to mind is if any translation is done of carriage returns and/or linefeeds. That could potentially change the index if CR/LF is mapped to just LF (as one example).

Sign In or Register to comment.