i am editing passsword entry in any random position cursor are auto selection next character for entry in ios xamarin?

S GAIKWAD 231 Reputation points
2021-06-17T13:39:42.63+00:00
namespace Sample.iOS.Renderers
{
    public class LoginEntryRenderer : EntryRenderer
    {
        public LoginEntryRenderer()
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.ShouldChangeCharacters += (textField, range, replacementString) =>
                {
                    string text = Control.Text;
                    var result = text.Substring(0, (int)range.Location) + replacementString + text.Substring((int)range.Location + (int)range.Length);
                    this.Element.Text = result;

                    if (e.NewElement != null)
                    {
                        if ((int)range.Length == 0)
                        {
                            e.NewElement.CursorPosition = (int)range.Location + 1;
                        }
                        else
                        {
                            e.NewElement.CursorPosition = (int)range.Location;
                        }
                    }
                    return false;
                };
            }
        }
    }
}

`

`strong text

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-06-18T06:52:50.35+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    When you edit the entry, there are two coniderations, delete or add characters, And shouldChangeCharactersInRange will be executed twice, please try to use Entry's textChange event.

       private void MyEntry_TextChanged(object sender, TextChangedEventArgs e)  
               {  
                   
         
                   var entry=sender as Entry;  
                   var newValue=e.NewTextValue;  
                   var oldValue=e.OldTextValue;  
                   var CursorPostion= entry.CursorPosition  ;  
                   if (oldValue==null)  
                   {  
                         //entry is empty  
                   }  
                   else if(newValue.Length> oldValue.Length)  
                   {  
                       //add value  
                       CursorPostion = entry.CursorPosition+2;  
                       if (CursorPostion< newValue.Length)  
                       {  
                           entry.CursorPosition = CursorPostion;  
                       }  
                         
                        
                   }  
                   else if( newValue.Length < oldValue.Length) {  
         
                       //delete value  
                       CursorPostion = entry.CursorPosition-2;  
                       if (CursorPostion >0 )  
                       {  
                           entry.CursorPosition = CursorPostion;  
                       }  
                       
                   }  
                    
               }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.