How to format window shapes Text box with thousands separator and decimal separator for entering numbers

I am new to Winforms and am trying to do something. I am using C #. I use Windows forms and I put 8 text fields in my form, all of them are numeric with decimal value. I like to do the results below. my decimal separator is a comma, and the thousands separator is a period. I have ever seen something like ##. ###, ## or something else, but I don’t remember .... Can anyone advise how to achieve this approach?

So the idea is that when I type 1234 and leave the focus out of the text field that it should format, and when I return to the text field again, the thousands separator should not format only the decimal separator.

I think I should use some events like LostFocus?

input result;

1234 1.234.00

12.34 12.34r>

12.34 12.34

1234567 1.234.567.00

12.34 12.34

12345.67 12.345.67

+4
source share
2 answers

In your case, LostFocus in the text box use:

textBox1.Text = string.Format("{0:#,##0.00}", double.Parse(textBox1.Text)); 

Before applying the above logic, make sure the text is double / integer or it will throw an exception. This decision is quite tough, tough.

If you want the format to be in a certain culture, and not in the current computer culture,

 textBox1.Text = string.Format(System.Globalization.CultureInfo.GetCultureInfo("id-ID"), "{0:#,##0.00}", double.Parse(textBox1.Text)); 

The above example is for Indonesia's currency format, in which a thousand separators use a period (".") Instead of a comma (",").

+7
source

Maybe you can use MaskedTextBox

http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx

You can customize the mask based on input length when focus is lost. Hope this will be helpful.

+1
source

All Articles