当用户在文本框中键入任意数量的现金时格式化货币值

时间:2018-05-24 18:53:56

标签: c# wpf

当用户输入任意数量的现金时,自动格式化货币值的最佳方法是什么?

例如,我有一个TextBox,如果用户键入“30”,它会考虑0.30 如果他输入“300”则认为是3.00。

1 个答案:

答案 0 :(得分:0)

创建一个文本框并在TextChanged事件

中处理它
<TextBox x:Name="money" Text="00.00" TextChanged="TextBox_TextChanged"/>

和代码

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (money.Text.Length == 3)
        {
            money.TextChanged -= TextBox_TextChanged;
            money.Text = money.Text.Replace("00.", "00.0");
            money.CaretIndex = money.Text.Length;
            money.TextChanged += TextBox_TextChanged;
        }
        if (money.Text.Length == 1)
        {
            money.TextChanged -= TextBox_TextChanged;
            if(money.Text.Contains(".")) money.Text = money.Text.Replace(".", "");
            money.Text = money.Text.Insert(0, "00.0");
            money.CaretIndex = money.Text.Length;
            money.TextChanged += TextBox_TextChanged;
        }
        else if (money.Text.StartsWith("00.0") && money.Text.Length == 6)
        {
            money.TextChanged -= TextBox_TextChanged;
            money.Text = money.Text.Replace("00.0", "00.");
            money.CaretIndex = money.Text.Length;
            money.TextChanged += TextBox_TextChanged;
        }
        else if (money.Text.StartsWith("00.") && money.Text.Length == 6)
        {
            money.TextChanged -= TextBox_TextChanged;
            money.Text = money.Text.Replace("00.", "0");
            money.Text = money.Text.Insert(money.Text.Length - 2, ".");
            money.CaretIndex = money.Text.Length;
            money.TextChanged += TextBox_TextChanged;
        }
        else if (money.Text.StartsWith("0") && money.Text.Length == 6)
        {
            money.TextChanged -= TextBox_TextChanged;
            money.Text = money.Text.Replace("0", "");
            if(money.Text.Contains(".")) money.Text = money.Text.Replace(".", "");
            money.Text = money.Text.Insert(money.Text.Length - 2, ".");
            money.CaretIndex = money.Text.Length;
            money.TextChanged += TextBox_TextChanged;
        }
        else if(!money.Text.StartsWith("0"))
        {
            money.TextChanged -= TextBox_TextChanged;
            if (money.Text.Length == 3 && money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00");
            if(money.Text.Length == 4 && money.Text.Contains(".")) money.Text = money.Text.Insert(0, "0");
            if(money.Text.Length == 1 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00.0");
            if(money.Text.Length == 2 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00.");
            if (money.Text.Length == 3 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "0");

            if (money.Text.Contains(".")) money.Text = money.Text.Replace(".", "");
            money.Text = money.Text.Insert(money.Text.Length - 2, ".");
            money.CaretIndex = money.Text.Length;
            money.TextChanged += TextBox_TextChanged;
        }
        else
        {
            money.TextChanged -= TextBox_TextChanged;
            if (money.Text.Length == 3 && money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00");
            if (money.Text.Length == 4 && money.Text.Contains(".")) money.Text = money.Text.Insert(0, "0");
            if (money.Text.Length == 1 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00.0");
            if (money.Text.Length == 2 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00.");
            if (money.Text.Length == 3 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "0");
            if (money.Text.Contains(".")) money.Text = money.Text.Replace(".", "");
            money.Text = money.Text.Insert(money.Text.Length - 2, ".");
            money.CaretIndex = money.Text.Length;
            money.TextChanged += TextBox_TextChanged;
        }
    }