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

ath*_*r99 1 c# string formatting

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

我有一个用于现金输入的文本框(例如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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

然而,结果非常奇怪.

另一个是这样的:

    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); *
          }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

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

Szy*_*mon 10

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

    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;
    }
Run Code Online (Sandbox Code Playgroud)


Gre*_*ate 9

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

它实际上假设您一次输入一位数,因此当您按" 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);
}
Run Code Online (Sandbox Code Playgroud)

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

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