0

Can I have space separator as thousand separator in textbox C#?

  • Example 1:

Write number 58972,56 into textbox and textbox displayed 58 972,56

  • Example 2:

Write number 2358972,56 into textBox and textbox displayed 2 358 972,56

Trevor
  • 7,777
  • 6
  • 31
  • 50
  • 1
    "Can I have space separator as thousand separatpr" of course you can, but you will need to implement it yourself. A starter could be to use the TextChanged event – Mong Zhu Jul 01 '20 at 13:58
  • Does this answer your question? [How would I separate thousands with space in C#](https://stackoverflow.com/questions/17527847/how-would-i-separate-thousands-with-space-in-c-sharp) – DrkDeveloper Jul 01 '20 at 14:05

2 Answers2

0

You can use the Validated event to format the input:

private void TextBox1_Validated(object sender, EventArgs e)
{
    TextBox txt = (TextBox)sender;
    if (Decimal.TryParse(txt.Text, out var d)) {
        txt.Text = d.ToString("# ### ### ### ### ##0.00");
    }
}

It uses the current culture to determine whether to display the decimal separator as decimal point or comma. You can also specify a specific culture:

txt.Text = d.ToString("# ### ### ### ### ##0.00", new CultureInfo("de"));

But in the format string itself always use a decimal point.


The standard way of formatting a TextBox in winforms is to work with data binding. In the binding properties you can specify a format string.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

You can clone the Current Culture, change its NumberFormatInfo.NumberGroupSeparator and, eventually, its NumberFormatInfo.CurrencyGroupSeparator and set the values you prefer.
When you Clone() a CultureInfo object, these properties are not read-only anymore.

The clone is writable even if the original CultureInfo is read-only. Therefore, the properties of the clone can be modified.

The cloned CultureInfo will retain all default values except what you change.
You can now use the modified CultureInfo where it's needed, without affecting the default Culture behavior.

In relation to string formatting, see Standard numeric format strings.

var culture = CultureInfo.CurrentCulture.Clone() as CultureInfo;
culture.NumberFormat.NumberGroupSeparator = " ";
culture.NumberFormat.CurrencyGroupSeparator = " ";

// Elsewhere
[TextBox1].Text = 12345678.89.ToString("N2", culture);

[TextBox2].Text = 12345678.89M.ToString("C", culture);

If you instead want to change the CurrentCulture behavior, assign the modified CultureInfo to the CultureInfo.CurrentCulture object:

CultureInfo.CurrentCulture = culture;

// elsewhere: same behavior, now the default
[TextBox1].Text = 12345678.89.ToString("N2");

Your TextBox could adopt a double-faced logic based on the culture selected.
When the text is validated, it will be formatted using the modified culture if defined or the default one:

private void textBox_Validating(object sender, CancelEventArgs e)
{
    var numberStyle = NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint;
    if (double.TryParse(textBox.Text, numberStyle, culture ?? CultureInfo.CurrentCulture, out double value)) {
        textBox.ForeColor = SystemColors.ControlText;
        textBox.Text = value.ToString("N", culture ?? CultureInfo.CurrentCulture);
    }
    else {
        // Notify, change text color, whatever fits
        textBox.ForeColor = Color.Red;
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • Hello, Can I ask what do these parameters mean "N2","C","N" in example method .ToString("N", culture). Thank you for your answer. – Ngoc Hai Tran Jul 02 '20 at 14:51
  • Sure. Those are [Standard numeric format strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings). These formats are used to specify/determine/force a specific output format (not different than `"yyyy/MM/dd"`, to format a DateTime for presentation). You can use them to obtain a precise format. `N2` means *number with 2 decimal places*, `N` means *number formatted using the `NumberFormat` and decimal places defined by the current culture*, as `C` (currency). You can fine-tune your formatted output with these specifiers. – Jimi Jul 02 '20 at 15:03