Money mask

Asked

Viewed 120 times

1

I need to have in an entry a money mask, I’m trying some things I got information, follows:

void lanceMask(object sender, EventArgs e)
    {
        var ev = e as TextChangedEventArgs;

        if (ev.NewTextValue != ev.OldTextValue)
        {
            var entry = (Entry)sender;
            string text = Regex.Replace(ev.NewTextValue, @"[^0-9]", "");

            text = text.PadRight(9);

            //Removendo todos os digitos excedentes 
            if (text.Length > 8)
            {
                text = text.Remove(8);
            }

            if (text.Length < 5)
            {
                text = text.Insert(1, ".").TrimEnd(new char[] { ' ', '.', '-', ',' });
            }
            else
            {
                text = text.Insert(5, ".").Insert(7, ",").TrimEnd(new char[] { ' ', '.', '-', ',' });
            }

            if (entry.Text != text)
                entry.Text = text;
        }
    }

I would like to be able to use the semicolon if it had 6 digits (ex: 1,400.00), if it is 7 digits (ex: 23,600.00), and so on.

1 answer

1


I have received an answer from a colleague, as others may need, as follows:

public void Mascara(object sender, EventArgs e)
{
  var ev = e as TextChangedEventArgs;
  if (ev.NewTextValue != ev.OldTextValue)
  {
    var entry = (Entry)sender;
    entry.TextChanged -= Mascara;
    string text = Regex.Replace(ev.NewTextValue.Trim(), @"[^0-9]", "");

  if (text.Length > 2)
  {
    text = text.Insert(text.Length - 2, ",");
  }

  if (text.Length > 6)
  {
    text = text.Insert(text.Length - 6, ".");
  }
  if (text.Length > 10)
  {
    text = text.Insert(text.Length - 10, ".");
  }

  if (entry.Text != text)
  entry.Text = text;
  entry.TextChanged += Mascara;
  }
}
  • 1

    Very good. Remembering that you can accept your own answer :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.