Textbox for price/cash/currency on C#

前端 未结 6 557
慢半拍i
慢半拍i 2020-12-15 15:10

I have a problem that is haunting me for a while. I tried some solutions but they didn\'t worked.

I have a textbox that is for cash input ($999,99 for example). Howe

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-15 15:40

    I think you will be better off when formatting when the user moves to the next control, e.g. like below. Otherwise it will be very confusing as the text will change itself as the user is typing:

        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;
        }
    

提交回复
热议问题