【发布时间】:2015-09-02 02:17:37
【问题描述】:
我试图在Visual Studio 2013 中为currency 制作一个Custom Input Mask
但是,这种类型的掩码有一个限制:9999,00。
我不会写像 99999999,00 这样的数字。
我想要一个可以处理任意数量的数字的mask
有可能吗?
【问题讨论】:
我试图在Visual Studio 2013 中为currency 制作一个Custom Input Mask
但是,这种类型的掩码有一个限制:9999,00。
我不会写像 99999999,00 这样的数字。
我想要一个可以处理任意数量的数字的mask
有可能吗?
【问题讨论】:
//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);
}
【讨论】:
这对我有用。不要创建自定义掩码,而是创建自定义 maskedTextbox。
即使使用正确的掩码,交付的 maskedTextBox 也很难让用户输入数据。 currencyTextbox 自动格式化/移动输入的值。
将该类添加到项目后,您会看到currencyTextBox 出现在您的工具箱中。然后只需根据您要存储的美元价值的大小为其设置一个掩码。据作者说,你用全0,我个人用“$000,000.00”
【讨论】:
Microsoft 文档中详细介绍了通过正则表达式应用掩码的标准方法:https://msdn.microsoft.com/en-us/library/ms234064.aspx 与您的情况相关,可能类似于:$\d{9}.00 希望这可能会有所帮助。
【讨论】: