如何格式化一个Windows窗体文本框与千位分隔符和十进制数字输入 [英] how to format a windows forms Textbox with thousand separator and decimal separtor for Numeric input
问题描述
我正在使用Windows窗体,我把我的窗体上的8个文本框,都是数字十进制值。
我想达到下面的结果。我的小数点分隔符是一个逗号,千位分隔符是一个点。
我曾经见过类似##。###,##或其他什么,但不记得....有人可以建议如何实现以下方法?所以这个想法是当我键入1234,离开它应该格式化的文本框的焦点,当我再次进入文本框时,千分离器不应该只格式化小数点分隔符。
我想我要使用一些像LostFocus这样的事件吗?
输入   ;结果 1234   ; 1.234,00
12.34 12,34
$ b $ 12,34 12,34
1234567 1.234.567,00
12,34 12,34
12345,67 12.345,67
在文本框中的LostFocus事件中,使用:
textBox1.Text = string。格式({0:#,## 0.00},double.Parse(textBox1.Text));
在应用上述逻辑之前,确保文本是双精度/整数,否则会抛出异常。这个解决方案相当苛刻,很难。
如果您希望格式是在特定的文化中,而不是您当前计算机的文化,那么
textBox1.Text = string.Format(System.Globalization.CultureInfo.GetCultureInfo(id-ID),{0:#,## 0.00}, double.Parse(textBox1.Text));上面的例子适用于印度尼西亚货币格式,其中千位分隔符使用点(。),而不是逗号(,)。
I'm newbie with Winforms and try to do something. I'm using C#.
I'm using windows forms and I've put a 8 textbox on my form, all are numeric with decimal value.
I like to achieve the results below. my decimal separator is a comma and thousand separator is a dot.
I've ever seen something like ##.###,## or whatever but don't remember.... Can someone advice how to achieve the below approach?
So the idea is when I type 1234 and leave the focus from the textbox it should format and when I get in the textbox back again the thousand separator should not format only the decimal separator.
I think I've to use some events like LostFocus?
input result
1234 1.234,00
12.34 12,34
12,34 12,34
1234567 1.234.567,00
12,34 12,34
12345,67 12.345,67
解决方案 On your LostFocus event in textbox, use:
textBox1.Text = string.Format("{0:#,##0.00}", double.Parse(textBox1.Text));
Make sure that the text is double / integer first before applying the above logic or it will throw exception. This solution is rather harsh, tough.
If you want the format to be in specific culture rather than your current computer's culture, then
textBox1.Text = string.Format(System.Globalization.CultureInfo.GetCultureInfo("id-ID"), "{0:#,##0.00}", double.Parse(textBox1.Text));
The above example is for Indonesian currency format, which thousand separator use dot (".") rather than comma (",").
这篇关于如何格式化一个Windows窗体文本框与千位分隔符和十进制数字输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!