简体   繁体   中英

As the user types: Separate numbers with commas, and format it into currency in C#

I have a textbox called textBox1.

Goal: As soon as the user types in textBox1, I want the program to convert the numbers into currency format.

Example: If the user typed 123456, I want the program to separate the numbers 123,456 like so.

在此处输入图片说明

i know it's late but try this one it should fix your problem

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
            {
                textBox1.Text = Convert.ToDouble(textBox1.Text).ToString("N0");
                textBox1.SelectionStart = textBox1.Text.Length;
            }
        }

Below is the basic approach, when the text changes convert it into a decimal then change the text to the string representation of the decimal.

textBox1.TextChanged += (s,e) =>
{
   var value = Decimal.Parse(textBox1.Text);
   textBox1.Text = value.ToString("C");
}

You should also check for illegal number in textBox. Take a look at Decimal.TryParse .

Upon research I came across this code. This code did exactly what i wanted.

    private void form_3_Load(object sender, EventArgs e)
    {
        textBox1.Text = "$0.00";
    }

    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);
        btn_test.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);
    }


    void tb_TextChanged(object sender, EventArgs e)
    {
        //Remove previous formatting, or the decimal check will fail
        string value = textBox1.Text.Replace(",", "").Replace("$", "");
        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
            textBox1.TextChanged -= tb_TextChanged;
            //Format the text as currency
            textBox1.Text = string.Format(CultureInfo.CreateSpecificCulture("en-US"), "{0:C2}", ul);
            textBox1.TextChanged += tb_TextChanged;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM