Money Mask

1

I need to have an entry in a cash mask, I'm trying to get some things I got from information,

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 a semicolon if I had 6 digits (ex: 1,400.00), if it is 7 digits (ex: 23,600.00), and so on.

    
asked by anonymous 21.05.2018 / 16:20

1 answer

1

I obtained a response from a colleague, as others may need, 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;
  }
}
    
23.05.2018 / 13:52