12

I'm new to Windows Forms and try to do something. I'm using C#.

I'm using Windows Forms and I've put eight textboxes on my form, and 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.... How can i 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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ethem
  • 2,888
  • 9
  • 42
  • 57
  • This is non-standard formatting. To achieve what you want you will have to run your input values (`string`s or numeric values) through your own formatting method. The reason for this is that you require custom behaviour which changes based on the input values in a non-standard way. – MoonKnight Mar 18 '13 at 10:20

2 Answers2

20

On your LostFocus event in the 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 an exception. This solution is rather harsh, tough.

If you want the format to be in a 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 the Indonesian currency format, in which the thousand separator use dot (".") rather than comma (",").

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fendy
  • 4,565
  • 1
  • 20
  • 25
  • 1
    it's not working, after entering any value and leaving the textbox it shows : #,##0 – ethem Mar 18 '13 at 09:42
  • with the ne format: 1145 becomes 1,145 and 11.45 becomes 11 and 50 remains 50 – ethem Mar 18 '13 at 10:16
  • Oh I have updated my answer again. Did not see your requirement of trailing zero – Fendy Mar 18 '13 at 10:49
  • exact what I need just 1 thing 1) in your code thousand separator is a comma it should be a dot AND decimal is dot it should be a comma... your code: 1,145.00 it should be 1.145,00 – ethem Mar 18 '13 at 12:10
2

Perhaps you could use the MaskedTextBox.

You could adjust the mask based on the input length when losing focus.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
stevepkr84
  • 1,637
  • 2
  • 13
  • 23
  • I tried maskedtextbox, with 999.999,99 and 000.000,00 but there is no freedom then and I have to put a number with 9 Masker or a space for 0 masker to just achieve 1,25. – ethem Mar 18 '13 at 10:18
  • It might work if you remove the mask on Enter, then on Leave, check the length and apply a Mask. – stevepkr84 Mar 18 '13 at 10:21