C#上的价格/现金/货币文本框

我有一个困扰我一段时间的问题。 我尝试了一些解决方案,但它们没有用。

我有一个用于现金输入的文本框(例如999,99美元)。 但是我需要自动输入“,”和“。” 正确显示值。

我试过两个解决方案。 其中之一是:

private void tx_ValorUnidade_TextChanged(object sender, EventArgs e) { string value = tx_ValorUnidade.Text.Replace(",", "").Replace("R$", ""); decimal ul; //Check we are indeed handling a number if (decimal.TryParse(value, out ul)) { //Unsub the event so we don't enter a loop tx_ValorUnidade.TextChanged -= tx_ValorUnidade_TextChanged; //Format the text as currency tx_ValorUnidade.Text = string.Format(System.Globalization.CultureInfo.CreateSpecificCulture("pt-BR"), "{0:C2}", ul); tx_ValorUnidade.TextChanged += tx_ValorUnidade_TextChanged; } } 

然而,结果非常奇怪。

另一个是这样的:

  private void tx_ValorUnidade_KeyUp(object sender, KeyEventArgs e) { if (!string.IsNullOrEmpty(tx_ValorUnidade.Text)) { System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US"); int valueBefore = Int32.Parse(tx_ValorUnidade.Text, System.Globalization.NumberStyles.AllowThousands); tx_ValorUnidade.Text = String.Format(culture, "{0:N0}", valueBefore); tx_ValorUnidade.Select(tx_ValorUnidade.Text.Length, 0); * } } 

这有一点有用,但是有一个问题:如果用户想要插入一些想法,如$ 10,00,它就不能。 它也会在5个数字后崩溃。

对于原始参考,我从这里得到了其他 问题的2个代码。

我该如何解决? 我使用的例子错了吗? 欢迎任何想法。

我认为当用户移动到下一个控件时格式化会更好,例如下面的内容。 否则会很困惑,因为文本会随着用户键入而自行更改:

  private void textBox1_Leave(object sender, EventArgs e) { Double value; if (Double.TryParse(textBox1.Text, out value)) textBox1.Text = String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:C2}", value); else textBox1.Text = String.Empty; } 

有些人可能希望在键入时实际格式化文本框。 所以这是我的解决方案,如果有人正在寻找一个。

它实际上假设您一次输入一位数,因此当您按“ 1 ”时它假定为“ $ 0.01 ”,当它们按“ 2 ”时它会假定为“ $ 0.12 ”,依此类推。

我在网上找不到任何关于格式化的信息。 它已经过测试,如果有任何错误让我知道。

 private void textBox1_TextChanged(object sender, EventArgs e) { //Remove previous formatting, or the decimal check will fail including leading zeros string value = textBox1.Text.Replace(",", "") .Replace("$", "").Replace(".", "").TrimStart('0'); decimal ul; //Check we are indeed handling a number if (decimal.TryParse(value, out ul)) { ul /= 100; //Unsub the event so we don't enter a loop textBox1.TextChanged -= textBox1_TextChanged; //Format the text as currency textBox1.Text = string.Format(CultureInfo.CreateSpecificCulture("en-US"), "{0:C2}", ul); textBox1.TextChanged += textBox1_TextChanged; textBox1.Select(textBox1.Text.Length, 0); } bool goodToGo = TextisValid(textBox1.Text); enterButton.Enabled = goodToGo; if (!goodToGo) { textBox1.Text = "$0.00"; textBox1.Select(textBox1.Text.Length, 0); } } private bool TextisValid(string text) { Regex money = new Regex(@"^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$"); return money.IsMatch(text); } 

为了使它看起来不错,我建议在表单加载时使用文本$ 0.00启动文本框,如下所示:

  private void Form1_Load(object sender, EventArgs e) { textBox1.Text = "$0.00"; textBox1.SelectionStart = inputBox.Text.Length; } 

只是稍微修改了GreatNates的答案。

 private bool KeyEnteredIsValid(string key) { Regex regex; regex = new Regex("[^0-9]+$"); //regex that matches disallowed text return regex.IsMatch(key); } 

并将此方法插入到文本框预览输入事件中,如下所示。

 private void TextBox1_PreviewTextInput(object sender, TextCompositionEventArgs e) { e.Handled = KeyEnteredIsValid(e.Text); } 

这样你就可以确保在输入任何内容时都不会犯任何错误。 您只能使用我的方法限制数字,而nates方法正在格式化您的字符串。

干杯。

我们也可以尝试一下。

 txtCost.Text = String.Format("{0:c2}", myObj.Cost); 

我也挣扎了好几个小时。 我尝试使用maskedTextBox,但用户输入文本只是笨拙。 我也不喜欢处理掩盖计算。 我也研究过使用数据绑定格式,但这看起来有些过分。

我最终的方式是不使用TextBox输入数字。 请改用NumericUpDown对象。 不需要转换,如果你愿意,可以在属性中设置小数点和数千个逗号;)我将增量设置为1000,因为我处理收入。

请注意,当有一个小数十进制且数量超过1000(即1,000.01)时,通过的.Text会有逗号,否则将丢弃小数和尾随0。

我还发现这个简短而且很好的解决方案效果很好但是使用numericUpDown是不必要的。 你可以把它放在休假活动上。

  Decimal val; if (Decimal.TryParse(TxtCosPrice.Text, out val)) TxtCosPrice.Text = val.ToString("C"); else MessageBox.Show("Oops! Bad input!"); 

这是我的解决方案,它只放点,而不是金钱符号。 希望可以帮助一些人。

  private void textBox1_KeyUp(object sender, KeyEventArgs e) { e.SuppressKeyPress = TextBox2Currency((TextBox)sender, e.KeyValue); } private bool TextBox2Currency(TextBox sender,int keyval) { if ((keyval >= 48 && keyval <= 58) || keyval == 46 || keyval == 8) { int pos = sender.SelectionStart; int oriLen = sender.Text.Length; string currTx = sender.Text.Replace(".", "").ToCurrency(); int modLen = currTx.Length; if (modLen != oriLen) pos += (modLen - oriLen); sender.Text = currTx; if ( pos>=0) sender.SelectionStart = pos; return false; } return true; }