MaskedTextBox Currency Input Mask Limits

21.7k views Asked by At

Im trying to make a Custom Input Mask for currency in Visual Studio 2013

enter image description here

But, this type of mask has a limit: 9999,00. I can't write numbers like 99999999,00.
I want a mask that works with any amount of numbers
Is it possible?

3

There are 3 answers

0
Alexander Bell On

The standard way of applying the mask via Regular Expresions is detailed in Microsoft documentation: https://msdn.microsoft.com/en-us/library/ms234064.aspx Pertinent to your case it could be something like: $\d{9}.00 Hope this may help.

1
blind Skwirl On

This worked for me. Instead of creating a custom mask, create a custom maskedTextbox.

Even with the correct mask, the delivered maskedTextBox is difficult for users to enter data. The currencyTextbox automatically formats/shifts the entered values.

https://blogs.msdn.microsoft.com/irenak/2006/03/21/sysk-87-a-better-maskedtextbox-for-currency-fields/

Once you add that class to your project, you'll see the currencyTextBox appear in your toolbox. Then just set a mask for it depending on how large a dollar value you want to store. According to the author, you use all 0s, I personally used "$000,000.00"

1
José Machado On
//Crie um textbox com o name txt_valor e atribua os eventos KeyPress,KeyUp e 
// Leave e uma string valor;
string valor;
    private void txt_valor_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!Char.IsDigit(e.KeyChar) && e.KeyChar != Convert.ToChar(Keys.Back))
        {
            if (e.KeyChar == ',')
            {
                e.Handled = (txt_valor.Text.Contains(","));
            }
            else
                e.Handled = true;
        }            
    }

    private void txt_valor_Leave(object sender, EventArgs e)
    {
        valor = txt_valor.Text.Replace("R$", "");
        txt_valor.Text = string.Format("{0:C}", Convert.ToDouble(valor));
    }

    private void txt_valor_KeyUp(object sender, KeyEventArgs e)
    {
        valor = txt_valor.Text.Replace("R$","").Replace(",","").Replace(" ","").Replace("00,","");
        if(valor.Length == 0)
        {
            txt_valor.Text = "0,00"+valor;
        }
        if(valor.Length == 1)
        {
            txt_valor.Text = "0,0"+valor;
        }
        if(valor.Length == 2)
        {
            txt_valor.Text = "0,"+valor;
        }
        else if(valor.Length >= 3)
        {
            if(txt_valor.Text.StartsWith("0,"))
            {
                txt_valor.Text = valor.Insert(valor.Length - 2,",").Replace("0,","");
            }
            else if(txt_valor.Text.Contains("00,"))
            {
                txt_valor.Text = valor.Insert(valor.Length - 2,",").Replace("00,","");
            }
            else
            {
                txt_valor.Text = valor.Insert(valor.Length - 2,",");
            }
        }           
        valor = txt_valor.Text;
        txt_valor.Text = string.Format("{0:C}", Convert.ToDouble(valor));
        txt_valor.Select(txt_valor.Text.Length,0);
    }