MaskedTextBox货币输入掩码限制

时间:2015-06-17 02:53:32

标签: c# winforms currency mask

我正试图在Custom Input Mask

中为currency制作Visual Studio 2013

enter image description here

但是,这种类型的面具有一个限制: 9999,00 。 我无法写出 99999999,00 等数字 我想要mask可以使用任意数量的数字 有可能吗?

3 个答案:

答案 0 :(得分:2)

通过常规表达式应用掩码的标准方法在Microsoft文档中有详细说明:https://msdn.microsoft.com/en-us/library/ms234064.aspx与您的案例相关,它可能类似于:$\d{9}.00希望这可能会有所帮助。

答案 1 :(得分:1)

这对我有用。而不是创建自定义蒙版,创建自定义maskedTextbox。

即使使用正确的掩码,用户也很难输入传递的maskedTextBox来输入数据。 currencyTextbox自动格式化/移动输入的值。

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

将该类添加到项目后,您将看到currencyTextBox出现在工具箱中。然后根据要存储的美元值的大小,为它设置一个掩码。根据作者的说法,你使用全0,我个人使用" $ 000,000.00"

答案 2 :(得分:-2)

//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);
    }
相关问题