Mask

ricardoventuraricardoventura Member ✭✭
edited March 28 in Xamarin.Forms

<Entry.Behaviors> <behavior:MaskedBehavior Mask="XXX,XX"/> </Entry.Behaviors>

Hello everyone,

Can somebody tell me if i can write a Mask from right to left,
Thank you!

Answers

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    @ricardoventura said:
    Can somebody tell me if i can write a Mask from right to left,

    Yes.

    Seriously though, if you have a left mask it is straightforward to edit that code. Post what you have for left masking, and what issues you are having making it go to the right.

  • ricardoventuraricardoventura Member ✭✭

    @AdamMeaney said:

    @ricardoventura said:
    Can somebody tell me if i can write a Mask from right to left,

    Yes.

    Seriously though, if you have a left mask it is straightforward to edit that code. Post what you have for left masking, and what issues you are having making it go to the right.

    I cant find how i turn to left my Mask, if you wanna help, i would appreciate:

    public class MaskedBehavior : Behavior
    {
    private string _mask = "";
    public string Mask
    {
    get => _mask;
    set
    {
    _mask = value;
    SetPositions();
    }
    }

        protected override void OnAttachedTo(Entry entry)
        {
            entry.TextChanged += OnEntryTextChanged;
            base.OnAttachedTo(entry);
        }
    
        protected override void OnDetachingFrom(Entry entry)
        {
            entry.TextChanged -= OnEntryTextChanged;
            base.OnDetachingFrom(entry);
        }
    
        IDictionary<int, char> _positions;
    
        void SetPositions()
        {
            if (string.IsNullOrEmpty(Mask))
            {
                _positions = null;
                return;
            }
    
            var list = new Dictionary<int, char>();
            for (var i = 0; i < Mask.Length; i++)
                if (Mask[i] != 'X')
                    list.Add(i, Mask[i]);
    
            _positions = list;
        }
    
        private void OnEntryTextChanged(object sender, TextChangedEventArgs args)
        {
            var entry = sender as Entry;
    
            var text = entry.Text;
    
            if (string.IsNullOrWhiteSpace(text) || _positions == null)
                return;
    
            if (text.Length > _mask.Length)
            {
                entry.Text = text.Remove(text.Length - 1);
                return;
            }
    
            foreach (var position in _positions)
                if (text.Length >= position.Key + 1)
                {
                    var value = position.Value.ToString();
                    if (text.Substring(position.Key, 1) != value)
                        text = text.Insert(position.Key, value);
                }
    
            if (entry.Text != text)
                entry.Text = text;
        }
    
    
    }
    
  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    Have you tried to edit your code at all? Do you know C# well?

    If you have this mask on and it is functional, making it work from the right (so that when starting with something like $00.00, if you hit 1, it shows $00.01) is trivial edits to the OnEntryTextChanged function.

    Do I have your issue correct on what you want it to do?

  • ricardoventuraricardoventura Member ✭✭

    @AdamMeaney said:
    Have you tried to edit your code at all? Do you know C# well?

    If you have this mask on and it is functional, making it work from the right (so that when starting with something like $00.00, if you hit 1, it shows $00.01) is trivial edits to the OnEntryTextChanged function.

    Do I have your issue correct on what you want it to do?

    Yes! Exactly! That's my issue

Sign In or Register to comment.