question

SWAPNILGAIKWAD-3135 avatar image
0 Votes"
SWAPNILGAIKWAD-3135 asked RobCaplan edited

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

 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


dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

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.


· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

CursorPosition this property doesn't exist. I am using Xamarin.Forms v2.5.0 and project dependency so can't update Xamarin.Forms. so, please give a solution for CursorPosition property

0 Votes 0 ·

Xamarin.Forms v2.5.0 ? Forms version is too old. If you cannot update forms version, we have to use custom renderer to achieve it.

0 Votes 0 ·

Ok sir Thanks,
Can I get a referral link?

0 Votes 0 ·

I test your code, I find your code will work sometimes.

0 Votes 0 ·

Yes sir, My Code work only one time, For first-time password edit then it works properly but in case of password clear all and again fill the password and again edit that case I am changing CursorPosition but the selection length is automatically changed. it automatically set the same as like cursor position.

0 Votes 0 ·
Show more comments